|
48 | 48 | <li><code>Node.val</code> is <code>0</code> or <code>1</code>.</li>
|
49 | 49 | </ul>
|
50 | 50 |
|
51 | | - |
52 | 51 | ## Solutions
|
53 | 52 |
|
| 53 | +DFS. |
| 54 | + |
54 | 55 | <!-- tabs:start -->
|
55 | 56 |
|
56 | 57 | ### **Python3**
|
57 | 58 |
|
58 | 59 | ```python
|
59 | | - |
| 60 | +# Definition for a binary tree node. |
| 61 | +# class TreeNode: |
| 62 | +# def __init__(self, val=0, left=None, right=None): |
| 63 | +# self.val = val |
| 64 | +# self.left = left |
| 65 | +# self.right = right |
| 66 | +class Solution: |
| 67 | + def sumRootToLeaf(self, root: TreeNode) -> int: |
| 68 | + def dfs(root, t): |
| 69 | + if root is None: |
| 70 | + return 0 |
| 71 | + t = (t << 1) | root.val |
| 72 | + if root.left is None and root.right is None: |
| 73 | + return t |
| 74 | + return dfs(root.left, t) + dfs(root.right, t) |
| 75 | + |
| 76 | + return dfs(root, 0) |
60 | 77 | ```
|
61 | 78 |
|
62 | 79 | ### **Java**
|
63 | 80 |
|
64 | 81 | ```java
|
| 82 | +/** |
| 83 | + * Definition for a binary tree node. |
| 84 | + * public class TreeNode { |
| 85 | + * int val; |
| 86 | + * TreeNode left; |
| 87 | + * TreeNode right; |
| 88 | + * TreeNode() {} |
| 89 | + * TreeNode(int val) { this.val = val; } |
| 90 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 91 | + * this.val = val; |
| 92 | + * this.left = left; |
| 93 | + * this.right = right; |
| 94 | + * } |
| 95 | + * } |
| 96 | + */ |
| 97 | +class Solution { |
| 98 | + public int sumRootToLeaf(TreeNode root) { |
| 99 | + return dfs(root, 0); |
| 100 | + } |
| 101 | + |
| 102 | + private int dfs(TreeNode root, int t) { |
| 103 | + if (root == null) { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + t = (t << 1) | root.val; |
| 107 | + if (root.left == null && root.right == null) { |
| 108 | + return t; |
| 109 | + } |
| 110 | + return dfs(root.left, t) + dfs(root.right, t); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### **C++** |
| 116 | + |
| 117 | +```cpp |
| 118 | +/** |
| 119 | + * Definition for a binary tree node. |
| 120 | + * struct TreeNode { |
| 121 | + * int val; |
| 122 | + * TreeNode *left; |
| 123 | + * TreeNode *right; |
| 124 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 125 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 126 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 127 | + * }; |
| 128 | + */ |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + int sumRootToLeaf(TreeNode* root) { |
| 132 | + return dfs(root, 0); |
| 133 | + } |
| 134 | + |
| 135 | + int dfs(TreeNode* root, int t) { |
| 136 | + if (!root) return 0; |
| 137 | + t = (t << 1) | root->val; |
| 138 | + if (!root->left && !root->right) return t; |
| 139 | + return dfs(root->left, t) + dfs(root->right, t); |
| 140 | + } |
| 141 | +}; |
| 142 | +``` |
65 | 143 |
|
| 144 | +### **Go** |
| 145 | + |
| 146 | +```go |
| 147 | +/** |
| 148 | + * Definition for a binary tree node. |
| 149 | + * type TreeNode struct { |
| 150 | + * Val int |
| 151 | + * Left *TreeNode |
| 152 | + * Right *TreeNode |
| 153 | + * } |
| 154 | + */ |
| 155 | +func sumRootToLeaf(root *TreeNode) int { |
| 156 | + var dfs func(root *TreeNode, t int) int |
| 157 | + dfs = func(root *TreeNode, t int) int { |
| 158 | + if root == nil { |
| 159 | + return 0 |
| 160 | + } |
| 161 | + t = (t << 1) | root.Val |
| 162 | + if root.Left == nil && root.Right == nil { |
| 163 | + return t |
| 164 | + } |
| 165 | + return dfs(root.Left, t) + dfs(root.Right, t) |
| 166 | + } |
| 167 | + |
| 168 | + return dfs(root, 0) |
| 169 | +} |
66 | 170 | ```
|
67 | 171 |
|
68 | 172 | ### **...**
|
|
0 commit comments