First, the topic
1, examining
2. Analysis
A binary tree is given to calculate the sum of the nodes that are formed from the root node to all the paths of the leaf nodes.
Second, the answer
1, Ideas:
Method One,
Recursive DFS is used to record all the paths from the root node to the leaf node, accumulating the values.
Public intsumnumbers (TreeNode root) {List<List<Integer>> resultlist =NewArraylist<list<integer>>(); Sumnumbershelper (Resultlist,NewArraylist<integer>(), root); intsum = 0; for(list<integer>list:resultlist) { intTMP = 0; for(Integer i:list) tmp= tmp * 10 +i; Sum+=tmp; } returnsum; } Private voidSumnumbershelper (list<list<integer>>resultlist, ArrayList<Integer>targetlist, TreeNode root) { if(Root = =NULL) {Resultlist.add (NewArraylist<>(TargetList)); } Else{targetlist.add (root.val); if(Root.left = =NULL&& Root.right = =NULL) {sumnumbershelper (resultlist, TargetList, root.left); } Else if(Root.left = =NULL){//avoid joining two timesSumnumbershelper (resultlist, TargetList, root.right); } Else if(Root.right = =NULL){//avoid joining two timesSumnumbershelper (resultlist, TargetList, root.left); } Else{sumnumbershelper (resultlist, TargetList, root.right); Sumnumbershelper (Resultlist, TargetList, root.left); } targetlist.remove (Targetlist.size ()-1); } }
Method Two,
Hierarchical traversal with two queue, where a queue record node, a queue record values from the root node to the path that the node passes through.
Public intsumNumbers2 (TreeNode root) {if(Root = =NULL) return0; intsum = 0; Queue<TreeNode> Nodequeue =NewLinkedlist<>(); Queue<Integer> Valqueue =NewLinkedlist<>(); Nodequeue.add (root); Valqueue.add (Root.val); while(!Nodequeue.isempty ()) {Root=Nodequeue.poll (); if(Root.left = =NULL&& Root.right = =NULL) {sum+=Valqueue.poll (); } Else{ BooleanFlag =false; intval = 0; if(Root.left! =NULL) {flag=true; Nodequeue.add (Root.left); Val=Valqueue.poll (); Valqueue.add (Val*10 +root.left.val); } if(Root.right! =NULL) {nodequeue.add (root.right); if(Flag = =false) Val=Valqueue.poll (); Valqueue.add (Val*10 +root.right.val); } } } returnsum; }
Method three, using Stack to preorder and record the value of the path
Public intsumNumbers4 (TreeNode root) {if(Root = =NULL) return0; Stack<TreeNode> Nodestack =NewStack<>(); Stack<String> Nodepath =NewStack<>(); Nodestack.push (root); Nodepath.push (Root.val+ ""); intsum = 0; while(!Nodestack.isempty ()) {TreeNode node=Nodestack.pop (); String Currentpath=Nodepath.pop (); if(Node.right! =NULL) {Nodestack.push (node.right); Nodepath.push (Currentpath+node.right.val); } if(Node.left! =NULL) {Nodestack.push (node.left); Nodepath.push (Currentpath+node.left.val); } if(Node.left = =NULL&& Node.right = =NULL) Sum+=integer.valueof (Currentpath); } returnsum; }
Method Four,
The and values of the paths are computed directly using recursion.
Public intsumnumbers (TreeNode root) {returnHelper (root, 0); } Private intHelper (TreeNode root,inti) {if(Root = =NULL) return0; if(Root.left = =NULL&& Root.right = =NULL) returnI * 10 +Root.val; returnHelper (Root.left, I * + root.val) + helper (root.right, I * 10 +root.val); }
129. Sum Root to Leaf Numbers