首页 > 其他 > 详细

[GeeksForGeeks] Write a program to delete a tree

时间:2017-08-16 11:14:18      阅读:239      评论:0      收藏:0      [点我收藏+]

Write a program to delete a tree.

 

Solution. 

To delete all tree nodes, we need to set all non-leaf nodes‘ children nodes to null. So for a given non-leaf node, 

set its left child node to null, then set its right child node to null, then set the reference of this node to null. 

This manifests a post order traversal of a given binary tree. 

 

 1 public class DeleteTree {
 2     public void deleteTree(TreeNode node) {
 3         if(node == null) {
 4             return;
 5         }
 6         deleteTree(node.left);
 7         deleteTree(node.right);
 8         node = null;
 9     }
10 }

 

Follow up question: Can you solve this problem without using recursion?

 

[GeeksForGeeks] Write a program to delete a tree

原文:http://www.cnblogs.com/lz87/p/7343087.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!