Skip to content

Commit 5f12d8b

Browse files
author
beck chen
committed
a few tree questions
1 parent 3a8359d commit 5f12d8b

File tree

6 files changed

+111
-1
lines changed

6 files changed

+111
-1
lines changed
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.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
*/
13+
14+
function postorderTraversal(root) {
15+
if (!root) { return []; }
16+
const results = [];
17+
18+
const q = [root];
19+
while (q.length > 0) {
20+
const currentNode = q.pop();
21+
results.push(currentNode.val);
22+
23+
if (currentNode.left) {
24+
q.push(currentNode.left);
25+
}
26+
27+
if (currentNode.right) {
28+
q.push(currentNode.right);
29+
}
30+
}
31+
32+
return results.reverse();
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/*
3+
class TreeNode {
4+
constructor(val) {
5+
this.val = val;
6+
this.left_ptr = null;
7+
this.right_ptr = null;
8+
}
9+
}
10+
*/
11+
12+
/**
13+
* @param {TreeNode} root Root of the input tree.
14+
* @return {TreeNode} Root of the output tree.
15+
*/
16+
17+
var flipUpsideDown = function (root) {
18+
if (!root) { return root; }
19+
20+
function helper(currentNode, prevNode) {
21+
if (!currentNode) {
22+
return prevNode;
23+
}
24+
25+
prevNode.left_ptr = null;
26+
const newRootNode = helper(currentNode.left_ptr, currentNode);
27+
28+
currentNode.right_ptr = prevNode;
29+
currentNode.left_ptr = prevNode.right_ptr;
30+
prevNode.right_ptr = null;
31+
32+
return newRootNode;
33+
}
34+
35+
const rootNode = helper(root.left_ptr, root);
36+
return rootNode;
37+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
14+
function countUnivalSubtrees(root) {
15+
let counter = 0;
16+
17+
if (!root) { return 0; }
18+
19+
function helper(node) {
20+
if (!node.left && !node.right) {
21+
counter++;
22+
return true;
23+
}
24+
25+
const leftValid = !node.left || (helper(node.left) && node.left.val === node.val);
26+
const rightValid = !node.right || (helper(node.right) && node.right.val === node.val);
27+
28+
if (leftValid && rightValid) {
29+
counter++;
30+
return true;
31+
}
32+
33+
return false;
34+
}
35+
36+
helper(root);
37+
38+
return counter;
39+
}

lessons/.DS_Store

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)