Same Tree [LeetCode]】的更多相关文章

Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and iterative solutions. Iterative solution: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo…
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. https://leetcode.com/problems/minimum-depth-of-binary-tree/ 题意就是给定一个二叉树,找出从根节点到叶子节点最低的高度,直…
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Solution:  bool helperBalanced(int…
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Summary: BFS or DFS with recursion. int minDepth(TreeNode *root) { if(root == NULL) ; if(r…
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 很早以前做的了  准…
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ struct TreeNode* built_bst(int* nums,int start,int end){ int mid; mid=(start+end)/2; if(start>end)return NULL; struct T…
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys…
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? Sulotion: Considers a BST as…
Problem Description: http://oj.leetcode.com/problems/same-tree/ class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { // Note: The Solution object is instantiated only once and is reused by each test case. if(p == NULL && q == NULL)…
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 v and w as the lowest node in T that has both v and w…