Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is asking for flatterning a binary tree to linked list by the pre-order, therefore we could flatten tree from the root. For each node, we link it with its n…
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ The basic idea is same to that for Construct Binary Tree from Inorder and Postorder Traversal. We solve it using a recursive function. First, we…
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ This problem can be easily solved using recursive method. By given the inorder and postorder lists of the tree, i.e. inorder[1..n] and postorde…
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar function to determine whether a sub-tree is balanced, if the tree is balanced, it also return the depth of the sub-tree. A tree T is balanced if the follow…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Subscribe to see which companies asked this question 解答 先序遍历同时把节点都堆到左边,…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下: /** * Definition for a…
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 第一个想法是先序遍历,然后按照访问顺序,添加右结点. public static void…
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the tree, and for each node we check if current_node.val > prev_node.val. The code is as follows. # Definition for a binary tree node # class TreeNode: #…
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder traversal of a binary search tree should be a sorted array. Therefore, we can compare each node with its previous node in the inorder to find the two…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 思路: 用先序遍历,得到的是从小到大的顺序.把先序遍历变形一下: void flatten(TreeNode* root) { if(NULL == root) return; vec…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路: 看上去很简单,使用二叉树的中序遍历就可以实现.但是题目的小曲点在于要在原tree上修改,如果将左儿子放在右儿子位置上,会丢失右子树,导致遍历失败. 解决方法有两种: 1.不…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the…
题目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 思路: 按照树的先序遍历顺序把节点串联起来即可. /** * Definition for a binary tree node. * function TreeNode(va…
深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    # @param {TreeNode} root    # @return {string[]}        resultList=[…
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def levelOrderBottom(self, root):   …
#-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    tmplist1=[]        def dfsDept…
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的话,这个耗时是很大的.O(n^2) ?差不多了! 菜逼啊!时间估计都错了!!! 时间是多少呢? while 最左侧的数,会不断被遍历!是这样的.大概会被遍历o(n)次 所以还是O(n^2)? 反正是复杂了. void flatten(struct TreeNode* root) { if(root…
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Hints: If you notice carefully in th…
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历,然后串成链表 主要思想就是:先递归对右子树进行链表化并记录,然后将root->right指向 左子树进行链表化后的头结点,然后一直向右遍历子树,连接上之前的右子树 /** * Definition for a binary tree node. * struct TreeNode { * int v…
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: 用stack. class Solution { public: void flatten(TreeNode *root) { if(!root) return; TreeNode *pnode = NULL; stack<TreeNode*> s; s.push(root); while(!s…
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 SOLUTION 1:使用递归解决,根据left是否为空,先连接left tree, 然后再连接右子树.使用一个t…
Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 这题的主要难点是"in-place".因为这个flatten的顺序是中.左.右(相当于中序遍历),所…
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5…
1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7…
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…
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: / \ / \ \ The flattened tree should look like: \ \ \ \ \ /** * Definition for a binary tree node. * struct TreeNode…
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二叉树展开成链表 [] (D:\dataStructure\Leetcode\114.png) 思路:将根节点与左子树相连,再与右子树相连.递归地在每个节点的左右孩子节点上,分别进行这样的操作. 代码 class Solution(object): def flatten(self, root): i…
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 click to show hints. Hints: If you notice carefully in t…
Flatten Binary Tree to Linked List My Submissions QuestionEditorial Solution Total Accepted: 81373 Total Submissions: 261933 Difficulty: Medium Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The fl…
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solution is easy, just use a stack storing the nodes not visited. Each iteration, pop a node and visited it, then push its right child and then left child in…