|
6 | 6 |
|
7 | 7 | <p>A binary tree is <em>univalued</em> if every node in the tree has the same value.</p> |
8 | 8 |
|
9 | | - |
10 | | - |
11 | 9 | <p>Return <code>true</code> if and only if the given tree is univalued.</p> |
12 | 10 |
|
13 | | - |
14 | | - |
15 | 11 | <p> </p> |
16 | 12 |
|
17 | | - |
18 | | - |
19 | 13 | <p><strong>Example 1:</strong></p> |
20 | 14 |
|
21 | 15 | <img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0965.Univalued%20Binary%20Tree/images/unival_bst_1.png" style="width: 265px; height: 172px;" /> |
|
28 | 22 |
|
29 | 23 | </pre> |
30 | 24 |
|
31 | | - |
32 | | - |
33 | 25 | <div> |
34 | 26 |
|
35 | 27 | <p><strong>Example 2:</strong></p> |
|
46 | 38 |
|
47 | 39 | </div> |
48 | 40 |
|
49 | | - |
50 | | - |
51 | 41 | <p> </p> |
52 | 42 |
|
53 | | - |
54 | | - |
55 | 43 | <p><strong>Note:</strong></p> |
56 | 44 |
|
57 | | - |
58 | | - |
59 | 45 | <ol> |
60 | 46 | <li>The number of nodes in the given tree will be in the range <code>[1, 100]</code>.</li> |
61 | 47 | <li>Each node's value will be an integer in the range <code>[0, 99]</code>.</li> |
62 | 48 | </ol> |
63 | 49 |
|
64 | | - |
65 | | - |
66 | 50 | ## Solutions |
67 | 51 |
|
68 | 52 | <!-- tabs:start --> |
69 | 53 |
|
70 | 54 | ### **Python3** |
71 | 55 |
|
72 | 56 | ```python |
73 | | - |
| 57 | +# Definition for a binary tree node. |
| 58 | +# class TreeNode: |
| 59 | +# def __init__(self, val=0, left=None, right=None): |
| 60 | +# self.val = val |
| 61 | +# self.left = left |
| 62 | +# self.right = right |
| 63 | +class Solution: |
| 64 | + def isUnivalTree(self, root: TreeNode) -> bool: |
| 65 | + def dfs(root): |
| 66 | + if root is None: |
| 67 | + return True |
| 68 | + if root.val != self.val: |
| 69 | + return False |
| 70 | + return dfs(root.left) and dfs(root.right) |
| 71 | + |
| 72 | + self.val = root.val |
| 73 | + return dfs(root) |
74 | 74 | ``` |
75 | 75 |
|
76 | 76 | ### **Java** |
77 | 77 |
|
78 | 78 | ```java |
| 79 | +/** |
| 80 | + * Definition for a binary tree node. |
| 81 | + * public class TreeNode { |
| 82 | + * int val; |
| 83 | + * TreeNode left; |
| 84 | + * TreeNode right; |
| 85 | + * TreeNode() {} |
| 86 | + * TreeNode(int val) { this.val = val; } |
| 87 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 88 | + * this.val = val; |
| 89 | + * this.left = left; |
| 90 | + * this.right = right; |
| 91 | + * } |
| 92 | + * } |
| 93 | + */ |
| 94 | +class Solution { |
| 95 | + private int val; |
| 96 | + |
| 97 | + public boolean isUnivalTree(TreeNode root) { |
| 98 | + val = root.val; |
| 99 | + return dfs(root); |
| 100 | + } |
| 101 | + |
| 102 | + private boolean dfs(TreeNode root) { |
| 103 | + if (root == null) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + if (root.val != val) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + return dfs(root.left) && dfs(root.right); |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### **C++** |
| 115 | + |
| 116 | +```cpp |
| 117 | +/** |
| 118 | + * Definition for a binary tree node. |
| 119 | + * struct TreeNode { |
| 120 | + * int val; |
| 121 | + * TreeNode *left; |
| 122 | + * TreeNode *right; |
| 123 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 124 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 125 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 126 | + * }; |
| 127 | + */ |
| 128 | +class Solution { |
| 129 | +public: |
| 130 | + int val; |
| 131 | + |
| 132 | + bool isUnivalTree(TreeNode* root) { |
| 133 | + val = root->val; |
| 134 | + return dfs(root); |
| 135 | + } |
| 136 | + |
| 137 | + bool dfs(TreeNode* root) { |
| 138 | + if (root == nullptr) return true; |
| 139 | + if (root->val != val) return false; |
| 140 | + return dfs(root->left) && dfs(root->right); |
| 141 | + } |
| 142 | +}; |
| 143 | +``` |
79 | 144 |
|
| 145 | +### **Go** |
| 146 | +
|
| 147 | +```go |
| 148 | +/** |
| 149 | + * Definition for a binary tree node. |
| 150 | + * type TreeNode struct { |
| 151 | + * Val int |
| 152 | + * Left *TreeNode |
| 153 | + * Right *TreeNode |
| 154 | + * } |
| 155 | + */ |
| 156 | +func isUnivalTree(root *TreeNode) bool { |
| 157 | + return dfs(root, root.Val) |
| 158 | +} |
| 159 | +
|
| 160 | +func dfs(root *TreeNode, val int) bool { |
| 161 | + if root == nil { |
| 162 | + return true |
| 163 | + } |
| 164 | + if root.Val != val { |
| 165 | + return false |
| 166 | + } |
| 167 | + return dfs(root.Left, val) && dfs(root.Right, val) |
| 168 | +} |
80 | 169 | ``` |
81 | 170 |
|
82 | 171 | ### **...** |
|
0 commit comments