随笔分类 - BinaryTree
[Leetcode 108]有序数组转BST二叉搜索树Convert Sorted Array to Binary Search Tree
摘要:题目 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Given an integer array nums where the elements are sorted in ascending or
阅读全文
[Leetcode 235/236]LCA二叉树最近公共祖先Lowest Common Ancestor of a Binary Tree
摘要:题目 给定二叉树和两个点,求两点的LCA最近公共祖先 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of L
阅读全文
[Leetcode 98]判断有效的二叉搜索树Validate Binary Search Tree
摘要:题目 https://leetcode.com/problems/validate-binary-search-tree 判断所给二叉树是否是二叉搜索树 二叉搜索树:其值left<root<right *[2,2,2]这种相等的情况也不是搜索树 Given the root of a binary
阅读全文
[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree
摘要:题目 求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+1(本身这个点 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given the root of a binary tree, return its maxi
阅读全文
[Leetcode 103]二叉树zigzag交叉遍历Binary Tree Zigzag Level Order Traversal
摘要:题目 二叉树zigzag遍历 层次遍历:始终左右 zigzag遍历:每层右左左右交替 Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from
阅读全文
[Leetcode 124]二叉树最大权重路径和 Binary Tree Maximum Path Sum
摘要:题目 与543相似,543是求最长路径,这是最大权重求和(路径不一定最长) A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge con
阅读全文
[Leetcode 543]二叉树的直径Diameter of Binary Tree
摘要:题目 二叉树的直径,即是求树中任意俩点间的最大距离(深度) Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the len
阅读全文
[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
摘要:【题目】 Given a binary tree, return the preordertraversal of its nodes' values. Example: 【思路】 有参考,好机智,使用堆栈压入右子树,暂时存储。 左子树遍历完成后遍历右子树。 【代码】
阅读全文
[Leetcode 108]有序数组转二叉搜索树Convert Sorted Array to Binary Search Tree
摘要:【题目】 给出的升序排序的数组,个数必为奇数,要求形成二叉搜索(平衡)树。 【思路】 辅助函数fun,[0,len]=>[0,mid-1]+[mid+1,len]。 当left>right,返回null。 【AC代码】
阅读全文
[Leetcode 105]*前序后序遍历形成树
摘要:so basically flag-start is size of the roots left subtree, therefore to get the start of right subtree you gotta get to the start of the first value o
阅读全文
[Leetcode 101]判断对称树 Symmetric Tree
摘要:【题目】 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is s
阅读全文
[Leetcode 100]判断二叉树相同 Same Tree
摘要:【题目】 判断二叉树是否相同。 【思路】 check函数。 p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p、q其中一方更短) p.val==q.val,值相同,继续迭代向左向右遍历check(p.left,q.left)&&ch
阅读全文
浙公网安备 33010602011771号