[GeeksForGeeks] Write a program to delete a tree

Posted Push your limit!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GeeksForGeeks] Write a program to delete a tree相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

[GeeksForGeeks] Diameter of a Binary Tree

平摊分析 Amortized Analysis ------geeksforgeeks翻译

geeksforgeeks@ Maximum Index (Dynamic Programming)

[GeeksForGeeks] Multiply a given integer by 3.5

分析循环 Analysis of Loops-------geeksforgeeks 翻译

c中的openmp并行递归函数