Skip to content

Commit 2bcd30d

Browse files
committed
add q814 java, cpp, go
1 parent a8d1402 commit 2bcd30d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

814-binary-tree-pruning/cpp/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10+
* right(right) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
TreeNode* pruneTree(TreeNode* root) {
16+
if (containsOneAndPrune(root)) {
17+
return root;
18+
}
19+
return NULL;
20+
}
21+
22+
bool containsOneAndPrune(TreeNode* root) {
23+
if (root == NULL) {
24+
return false;
25+
}
26+
bool contains = root->val == 1;
27+
bool left = containsOneAndPrune(root->left);
28+
bool right = containsOneAndPrune(root->right);
29+
30+
if (!left) {
31+
root->left = NULL;
32+
}
33+
34+
if (!right) {
35+
root->right = NULL;
36+
}
37+
38+
return contains || left || right;
39+
}
40+
};

814-binary-tree-pruning/go/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
func pruneTree(root *TreeNode) *TreeNode {
12+
if pruneTreeAndReturn(root) {
13+
return root
14+
}
15+
return nil
16+
}
17+
18+
func pruneTreeAndReturn(root *TreeNode) bool {
19+
if root == nil {
20+
return false
21+
}
22+
23+
self := root.Val == 1
24+
left := pruneTreeAndReturn(root.Left)
25+
right := pruneTreeAndReturn(root.Right)
26+
27+
if !left {
28+
root.Left = nil
29+
}
30+
31+
if !right {
32+
root.Right = nil
33+
}
34+
35+
return self || left || right
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
3+
* right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left,
4+
* TreeNode right) { this.val = val; this.left = left; this.right = right; } }
5+
*/
6+
class Solution {
7+
public TreeNode pruneTree(TreeNode root) {
8+
if (pruneAndFind(root)) {
9+
return root;
10+
}
11+
return null;
12+
}
13+
14+
public boolean pruneAndFind(TreeNode root) {
15+
if (root == null) {
16+
return false;
17+
}
18+
19+
boolean self = root.val == 1;
20+
boolean left = pruneAndFind(root.left);
21+
boolean right = pruneAndFind(root.right);
22+
23+
if (!left) {
24+
root.left = null;
25+
}
26+
27+
if (!right) {
28+
root.right = null;
29+
}
30+
31+
return self || left || right;
32+
}
33+
}

0 commit comments

Comments
 (0)