By given a binary tree, and a root node, find the deepest node of this tree. We have way to create node: function createNode(val, left = null, right = null) { return { val, left, addLeft(leftKey) { return (this.left = leftKey ? createNode(leftKey) :…
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if (root == NULL) { return res; } queue<TreeNode*,int>> q; q.push(make_pair(root,0)); while(!q.empty()){ TreeNode* node=q.f…
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. Notice Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded…
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2     3 \  5 All root-to-leaf paths are: [  "1->2->5",  "1->3"] LeetCode上的原题,请参见我之前的博客Binary Tree Paths. 解法一: class Solution {…
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [ "1->2->5", "1->3" ] As mentioned in the problem we want to get all root-to-leaf…
I did it in a recursive way. There is another iterative way to do it. I will come back at it later. /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val…
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. For example,Given 1 / \ 2 5 / \ \ 3 4 6   The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6…
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val;…
We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a 1 has been removed. (Recall that the subtree of a node X is X,…
We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not containing a 1 has been removed. (Recall that the subtree of a node X is X,…