[题目描述] For the given binary tree, return a deep copy of it. 深度复制一个二叉树,给定一个二叉树,返回一个他的克隆品. [题目链接] www.lintcode.com/en/problem/clone-binary-tree/ [题目解析] 假设有如下链表: |---------------| |                 v 1  --> 2 --> 3 --> 4 节点1的random指向了3.首先我们可以通过next遍…
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3},return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 这是一道LeetCode中标记为Hard的题.事实上如果没有限…
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ 2 3 / \ 4 5 return the new binary tree with same structure and same value: 1 / \ 2 3 / \ 4 5 解题:链表复制.递归解法比较简单,代码如下: /** * Definition of TreeNode: * pu…
Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has bot…
Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution…
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar…
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如LeetCode题目 104. Maximum Depth of Binary Tree: // 104. Maximum Depth of Binary Tree int maxDepth(TreeNode* root) { ; +max(maxDepth(root->left),maxDepth(roo…
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: public boolean isBalanced(TreeNode root) { boolean[] balanced = {true}; height(root, balanced); return balanced[0]; } private int height(TreeNode node,…
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这个树,把map里面填值,遍历结束后,再遍历这个map,把每层的平均数放到数组里,最后数组转为list返回,不用考虑list里的排序了. Java实现: /** * Definition for a binary tree node. * public class TreeNode { * int v…
[Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}&…