Skip to content

Commit 0d332c4

Browse files
committed
Diameter of Binary tree + Balanced binary tree
1 parent 6917bf7 commit 0d332c4

File tree

4 files changed

+440
-0
lines changed

4 files changed

+440
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 110. Balanced binary tree
2+
// Topics: 'Tree', 'Binary Tree', 'Depth-First Search'
3+
4+
// Given a binary tree, determine if it is
5+
6+
// Example 1:
7+
8+
// Input: root = [3,9,20,null,null,15,7]
9+
// Output: true
10+
11+
// Example 2:
12+
13+
// Input: root = [1,2,2,3,3,null,null,4,4]
14+
// Output: false
15+
16+
// Example 3:
17+
18+
// Input: root = []
19+
// Output: true
20+
21+
// Constraints:
22+
23+
// The number of nodes in the tree is in the range [0, 5000].
24+
// -104 <= Node.val <= 104
25+
26+
package balancedbinarytree
27+
28+
type TreeNode struct {
29+
Right *TreeNode
30+
Left *TreeNode
31+
Val int
32+
}
33+
34+
func isBalanced(root *TreeNode) bool {
35+
return height(root) != -1
36+
}
37+
38+
func height(root *TreeNode) int {
39+
if root == nil {
40+
return 0
41+
}
42+
left := height(root.Left)
43+
if left == -1 {
44+
return left
45+
}
46+
right := height(root.Right)
47+
if right == -1 {
48+
return right
49+
}
50+
if abs(left-right) > 1 {
51+
return -1
52+
}
53+
return max(left, right) + 1
54+
}
55+
56+
func abs(x int) int {
57+
if x < 0 {
58+
return -x
59+
}
60+
return x
61+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package balancedbinarytree
2+
3+
import "testing"
4+
5+
func TestIsBalanced(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
root *TreeNode
9+
want bool
10+
}{
11+
{
12+
name: "balanced with deeper left subtree",
13+
root: &TreeNode{
14+
Val: 1,
15+
Left: &TreeNode{
16+
Val: 2,
17+
Left: &TreeNode{
18+
Val: 4,
19+
Left: &TreeNode{Val: 8},
20+
},
21+
Right: &TreeNode{Val: 5},
22+
},
23+
Right: &TreeNode{
24+
Val: 3,
25+
Left: &TreeNode{Val: 6},
26+
},
27+
},
28+
want: true,
29+
},
30+
{
31+
name: "example 1 - balanced tree",
32+
root: &TreeNode{
33+
Val: 3,
34+
Left: &TreeNode{Val: 9},
35+
Right: &TreeNode{
36+
Val: 20,
37+
Left: &TreeNode{Val: 15},
38+
Right: &TreeNode{Val: 7},
39+
},
40+
},
41+
want: true,
42+
},
43+
{
44+
name: "example 2 - unbalanced tree",
45+
root: &TreeNode{
46+
Val: 1,
47+
Left: &TreeNode{
48+
Val: 2,
49+
Left: &TreeNode{
50+
Val: 3,
51+
Left: &TreeNode{Val: 4},
52+
},
53+
Right: &TreeNode{
54+
Val: 3,
55+
Right: &TreeNode{Val: 4},
56+
},
57+
},
58+
Right: &TreeNode{Val: 2},
59+
},
60+
want: false,
61+
},
62+
{
63+
name: "example 3 - empty tree",
64+
root: nil,
65+
want: true,
66+
},
67+
{
68+
name: "single node",
69+
root: &TreeNode{Val: 1},
70+
want: true,
71+
},
72+
{
73+
name: "left skewed unbalanced",
74+
root: &TreeNode{
75+
Val: 1,
76+
Left: &TreeNode{
77+
Val: 2,
78+
Left: &TreeNode{
79+
Val: 3,
80+
},
81+
},
82+
},
83+
want: false,
84+
},
85+
{
86+
name: "right skewed unbalanced",
87+
root: &TreeNode{
88+
Val: 1,
89+
Right: &TreeNode{
90+
Val: 2,
91+
Right: &TreeNode{
92+
Val: 3,
93+
},
94+
},
95+
},
96+
want: false,
97+
},
98+
{
99+
name: "two nodes left",
100+
root: &TreeNode{
101+
Val: 1,
102+
Left: &TreeNode{Val: 2},
103+
},
104+
want: true,
105+
},
106+
{
107+
name: "two nodes right",
108+
root: &TreeNode{
109+
Val: 1,
110+
Right: &TreeNode{Val: 2},
111+
},
112+
want: true,
113+
},
114+
{
115+
name: "complete binary tree",
116+
root: &TreeNode{
117+
Val: 1,
118+
Left: &TreeNode{
119+
Val: 2,
120+
Left: &TreeNode{Val: 4},
121+
Right: &TreeNode{Val: 5},
122+
},
123+
Right: &TreeNode{
124+
Val: 3,
125+
Left: &TreeNode{Val: 6},
126+
Right: &TreeNode{Val: 7},
127+
},
128+
},
129+
want: true,
130+
},
131+
{
132+
name: "balanced with different heights",
133+
root: &TreeNode{
134+
Val: 1,
135+
Left: &TreeNode{
136+
Val: 2,
137+
Left: &TreeNode{Val: 3},
138+
},
139+
Right: &TreeNode{Val: 4},
140+
},
141+
want: true,
142+
},
143+
{
144+
name: "unbalanced subtree",
145+
root: &TreeNode{
146+
Val: 1,
147+
Left: &TreeNode{
148+
Val: 2,
149+
Left: &TreeNode{
150+
Val: 3,
151+
Left: &TreeNode{Val: 4},
152+
},
153+
},
154+
Right: &TreeNode{
155+
Val: 5,
156+
Left: &TreeNode{Val: 6},
157+
},
158+
},
159+
want: false,
160+
},
161+
{
162+
name: "balanced asymmetric tree",
163+
root: &TreeNode{
164+
Val: 1,
165+
Left: &TreeNode{
166+
Val: 2,
167+
Right: &TreeNode{Val: 3},
168+
},
169+
Right: &TreeNode{
170+
Val: 4,
171+
Left: &TreeNode{Val: 5},
172+
},
173+
},
174+
want: true,
175+
},
176+
}
177+
178+
for _, tt := range tests {
179+
t.Run(tt.name, func(t *testing.T) {
180+
got := isBalanced(tt.root)
181+
if got != tt.want {
182+
t.Errorf("isBalanced() = %v, want %v", got, tt.want)
183+
}
184+
})
185+
}
186+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 543. Diameter of Binary tree
2+
// Topics: 'Tree', 'Binary Tree', 'Depth-First Search'
3+
4+
// Given the root of a binary tree, return the length of the diameter of the tree.
5+
6+
// The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
7+
8+
// The length of a path between two nodes is represented by the number of edges between them.
9+
10+
// Example 1:
11+
12+
// Input: root = [1,2,3,4,5]
13+
// Output: 3
14+
// Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
15+
16+
// Example 2:
17+
18+
// Input: root = [1,2]
19+
// Output: 1
20+
21+
// Constraints:
22+
23+
// The number of nodes in the tree is in the range [1, 104].
24+
// -100 <= Node.val <= 100
25+
26+
package diameterofbinarytree
27+
28+
func diameterOfBinaryTree(root *TreeNode) int {
29+
var max int
30+
diameter(root, &max)
31+
return max
32+
}
33+
34+
func diameter(root *TreeNode, m *int) int {
35+
if root == nil {
36+
return 0
37+
}
38+
left := diameter(root.Left, m)
39+
right := diameter(root.Right, m)
40+
*m = max(*m, left+right)
41+
return max(left, right) + 1
42+
}
43+
44+
type TreeNode struct {
45+
Right *TreeNode
46+
Left *TreeNode
47+
Val int
48+
}

0 commit comments

Comments
 (0)