Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a7e3a3

Browse files
committed
add 129
1 parentaeffd77 commit8a7e3a3

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

‎Medium/129-sumRootToLeafNumbers.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
*@param {TreeNode} root
10+
*@return {number}
11+
*/
12+
varsumNumbers=function(root){
13+
if(!root)return0;
14+
vartotal=[];
15+
helper(root,0,total);
16+
varsum=0;
17+
for(vari=0;i<total.length;i++){
18+
sum+=total[i];
19+
}
20+
returnsum;
21+
};
22+
23+
varhelper=function(root,sum,total){
24+
sum=10*sum+root.val;
25+
if(root.left===null&&root.right===null){
26+
total.push(sum);
27+
return;
28+
}
29+
if(root.left){
30+
helper(root.left,sum,total);
31+
}
32+
if(root.right){
33+
helper(root.right,sum,total);
34+
}
35+
};
36+
37+
// a better and more concise solution
38+
varsumNumbers=function(root){
39+
returnhelper(root,0);
40+
};
41+
42+
varhelper=function(root,sum){
43+
if(!root)return0;
44+
if(root.left===null&&root.right===null){
45+
return10*sum+root.val;
46+
}
47+
returnhelper(root.left,10*sum+root.val)
48+
+helper(root.right,10*sum+root.val);
49+
};

‎Medium/144-binaryTreePreorder.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ var preorderTraversal = function(root) {
1818
order.push(root.val);
1919
if(root.right)rightNodes.push(root.right);
2020
root=root.left;
21-
}elseroot=rightNodes.pop();
21+
}else{
22+
root=rightNodes.pop();
23+
}
2224
}
2325
returnorder;
2426
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
*[116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) -[Solution](./Medium/116-PopulatingNextRightPointersinEachNode.js)
8888
*[121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) -[Solution](./Medium/121-bestTimeToBuySellStock.js)
8989
*[122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) -[Solution](./Medium/122-bestTimeToBuySellStockII.js)
90+
*[129. Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) -[Solution](./Medium/129-sumRootToLeafNumbers.js)
9091
*[136. Single Number](https://leetcode.com/problems/single-number/) -[Solution](./Medium/136-singleNumber.js)
9192
*[141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) -[Solution](./Medium/141-linkedListCycle.js)
9293
*[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -[Solution](./Medium/144-binaryTreePreorder.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp