<LeetCode OJ> 100. Same Tree】的更多相关文章

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 很简单,直接上代码: /** * Definition for a binary tree node. * public class…
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Tags: Depth-first Search 分析 很基本的一道深度优…
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traversal of a binary tree is a classic problem, the recursive way to solve it is really straightforward, the pseudo-code is as follows. RECURSIVE-POST-ORDE…
Problem Link: https://oj.leetcode.com/problems/same-tree/ The following recursive version is accepted but the iterative one is not accepted... # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left =…
Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse the tree level by level. For each level, we construct an array of values of the length 2^depth, and check if this array is symmetric. The tree is sym…
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree level by level using BFS method. # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # se…
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS from the root and for each level insert a list of values into the result. # Definition for a binary tree node # class TreeNode: # def __init__(self, x):…
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from the tree root to traverse the tree level by level. The python code is as follows. # Definition for a binary tree node # class TreeNode: # def __init__(s…
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a binary tree, there must exists a node N in P such that N is the ancestor node of all other nodes in P. We call such N as the root of P, or P roots at N. T…
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…