今天分享的题目来源于 LeetCode 第 105 号问题:重建二叉树。 题目链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 一、题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 例如,给...
link:[https://leetcode.com/explore/learn/card/data-structure-tree/134/traverse-a-tree/928/] 递归解法: #Definition for a binary tree node.#class TreeNode(object):#def __init__(self, x):#self.val = x#self.left = None#self.right = NoneclassSolution(object):defsolve(self,root):ifroo...
https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return thepostordertraversal of its nodes' values. For example: Given binary tree{1,#,2,3}, 1 \ 2 / 3 return[3,2,1]. Note:Recursive solution is trivial, could you do it iteratively? 后续遍历二叉树,...
力扣leetcode-cn.com/problems/diameter-of-binary-tree/ 题目描述 给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。 示例: 给定二叉树 1 / \ 2 3 / \ 4 5 返回3, 它的长度是路径 [4,2,1,3] 或者 [5...
a binary tree in which the left and right subtrees of every node differ in height by no more than 1. 英文版地址 leetcode.com/problems/b 中文版描述 给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 ...
此题来自leetcode https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree{1,#,2,3}, 代码语言:javascript 复制 1\2/3
Can you solve this real interview question? Binary Tree Postorder Traversal - Given the root of a binary tree, return the postorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [3,2,1] Explanation: [https://assets
题目地址:https://leetcode-cn.com/problems/binary-tree-upside-down/ 题目描述 Given a binary tree where all therightnodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right ...
难度 易 来源 https://leetcode.com/problems/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 ...
leetcode -- Balanced Binary Tree -- 简单重点 https://leetcode.com/problems/balanced-binary-tree/ 求depth,recursive就行 class Solution(object): def depth(self, root): if not root: return 0 return max(self.depth(root.left), self.depth(root.right)) + 1...