Node Depths of BinaryTree

refer to : https://www.algoexpert.io/questions/Node%20Depths


 

1. 问题描述。

二叉树的结点的深度(depth)定义为: 当前结点到二叉树的根的距离。 给定一个二叉树,求所有结点的深度的和。

 

2. 分析

每个结点的深度等于它的上一层结点的深度+ 1

 

3. recursive method。

# average case: balanced tree
# O(n) time | O(h) space, h is the height of the binary tree

 1 import java.util.*;
 2 
 3 class Program {
 4 
 5   public static int nodeDepths(BinaryTree root) {
 6     return helper(root, 0);
 7   }
 8     
 9   public static int helper(BinaryTree root, int depth){
10     if(root == null){
11        return 0;
12     }
13     return helper(root.left, depth + 1) + helper(root.right, depth+1) + depth;
14  }
15 
16   static class BinaryTree {
17     int value;
18     BinaryTree left;
19     BinaryTree right;
20 
21     public BinaryTree(int value) {
22       this.value = value;
23       left = null;
24       right = null;
25     }
26   }
27 }
 1 def nodeDepths(root,depth = 0 ):
 2     if root is None:
 3         return 0
 4     return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1)
 5     
 6 
 7 
 8 # This is the class of the input binary tree.
 9 class BinaryTree:
10     def __init__(self, value):
11         self.value = value
12         self.left = None
13         self.right = None

 

4. iterative method

# average case: balanced tree
# O(n) time | O(h) space, h is the height of the binary tree

 1 import java.util.*;
 2 
 3 class Program {
 4 
 5   public static int nodeDepths(BinaryTree root) {
 6     int sumOfDepth = 0;
 7         List<Level> stack = new ArrayList<Level>();
 8         stack.add(new Level(root,0));
 9         while(stack.size()>0){
10             Level top = stack.remove(stack.size()-1);
11             BinaryTree node = top.root;
12             int depth = top.depth;
13             if(node == null) continue;
14             sumOfDepth += depth;
15             stack.add(new Level(node.left, depth + 1));
16             stack.add(new Level(node.right, depth + 1));
17         }
18         return sumOfDepth;
19   }
20     
21     static class Level{
22         public BinaryTree root;
23         int depth;
24         
25         public Level(BinaryTree root, int depth){
26             this.root = root;
27             this.depth = depth;
28         }
29     }
30 
31   static class BinaryTree {
32     int value;
33     BinaryTree left;
34     BinaryTree right;
35 
36     public BinaryTree(int value) {
37       this.value = value;
38       left = null;
39       right = null;
40     }
41   }
42 }
 1 def nodeDepths(root):
 2     sumOfDepths = 0
 3     stack = [{"node":root, "depth":0}]
 4     while len(stack)>0:
 5         nodeInfo = stack.pop()
 6         node, depth = nodeInfo["node"], nodeInfo["depth"]
 7         if node is None:
 8             continue
 9         sumOfDepths += depth
10         stack.append({"node":node.left, "depth":depth+1})
11         stack.append({"node":node.right, "depth":depth+1})
12     return sumOfDepths
13 
14 
15 # This is the class of the input binary tree.
16 class BinaryTree:
17     def __init__(self, value):
18         self.value = value
19         self.left = None
20         self.right = None

 

上一篇:hdu1560DNA sequence(IDA*)


下一篇:1372 二叉树中的最长交错路径