![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二叉树
HELLO_蓝猫
这个作者很懒,什么都没留下…
展开
-
Leetcode 94. Binary Tree Inorder Traversal
送分题 没什么说的Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3], 1 \ 2 / 3/** * Definition for a binary tree node. * pub...原创 2018-03-22 21:48:57 · 97 阅读 · 0 评论 -
Leetcode 112. Path Sum
112. Path SumDescriptionHintsSubmissionsDiscussSolutionPick OneGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the...原创 2018-05-08 11:03:48 · 109 阅读 · 0 评论 -
Leetcode 113. Path Sum II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri...原创 2018-05-08 14:45:52 · 152 阅读 · 0 评论 -
Leetcode 116. Populating Next Right Pointers in Each Node
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) ...原创 2018-06-06 13:13:39 · 95 阅读 · 0 评论 -
Leetcode 117. Populating Next Right Pointers in Each Node II
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) ...原创 2018-06-06 13:24:36 · 94 阅读 · 0 评论 -
Leetcode 129. Sum Root to Leaf Numbers
解法:回溯法,一个vector去存储每一层的节点,如果是根节点计算结果,反之继续,没用过一个节点要pop出来回溯。Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->...原创 2018-06-14 22:09:35 · 123 阅读 · 0 评论 -
173. Binary Search Tree Iterator
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class BSTIte...原创 2018-07-11 15:14:21 · 215 阅读 · 0 评论 -
Leetcode 863. All Nodes Distance K in Binary Tree
解法:便利所有除了目标节点分支的节点,标记他们的爸爸和自己是左是右。然后从目标节点向外便利。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val...原创 2018-07-07 22:03:20 · 632 阅读 · 0 评论 -
Leetcode 199. Binary Tree Right Side View
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { int...原创 2018-08-28 17:15:13 · 106 阅读 · 0 评论 -
Leetcode 230 Kth Smallest Element in a BST
Given a binary search tree, write a functionkthSmallestto find thekth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Example 1:Input: root = [3,...原创 2019-03-07 10:58:46 · 111 阅读 · 0 评论 -
LRU缓存 Leetocde146. LRU Cache
class Node{public: int key_t; int value_t; Node(int key,int value):key_t(key),value_t(value) { }};class LRUCache {public: list<Node> cache;//缓存 双向链表 方便插入...原创 2019-03-14 21:18:39 · 138 阅读 · 0 评论 -
Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
235.Lowest Common Ancestor of a Binary Search TreeEasy100079FavoriteShareGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the...原创 2019-05-17 14:27:00 · 123 阅读 · 0 评论 -
LeetCode 110. Balanced Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution{ publ...原创 2018-05-04 15:19:23 · 118 阅读 · 0 评论 -
Leetcode 109. Convert Sorted List to Binary Search Tree
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *//** * Definition for a binary tree node. * pub...原创 2018-05-04 14:06:35 · 122 阅读 · 0 评论 -
Leetcode 108. Convert Sorted Array to Binary Search Tree
解法:永远以中点为根,左右划分,之后递归。将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。示例:给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树: 0 / \ -3 ...原创 2018-04-26 16:51:57 · 120 阅读 · 0 评论 -
Leetcode 95. Unique Binary Search Trees II
解法:二叉搜素树,想法是,把每一个数当根结点,然后去划分左右子树,最开始没有想好如何去吧结果放到数组中,只关注如何区分左右子书,重写方法,还是同样的类型,用来算每个节点的可能性,每个点都可能,然后保留当前点,左右划分,分完之后,不能划分只有两种,一种只有一个,一种是null,排列组合所有左右节点可能,放到ans。/** * Definition for a binary tree node. ...原创 2018-03-25 16:16:05 · 94 阅读 · 0 评论 -
Leetcode 100. Same Tree
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:In...原创 2018-04-02 13:26:35 · 97 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { int...原创 2018-03-29 16:07:04 · 111 阅读 · 0 评论 -
Leetcode 104. Maximum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2018-04-18 11:05:04 · 108 阅读 · 0 评论 -
Leetcode 102. Binary Tree Level Order Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2018-04-20 15:22:38 · 103 阅读 · 0 评论 -
Leetcode 103. Binary Tree Zigzag Level Order Traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2018-04-20 16:23:55 · 87 阅读 · 0 评论 -
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
解法:1.先序中第一个是根 2.中序根据顶点分左右两枝 3.先序先访问顶点 中序先访问左字数原创 2018-04-20 16:48:49 · 99 阅读 · 0 评论 -
Leetcode 101. Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2018-04-25 15:18:30 · 154 阅读 · 0 评论 -
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { publ...原创 2018-04-25 16:56:37 · 101 阅读 · 0 评论 -
Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { //和上一...原创 2018-04-25 19:18:03 · 170 阅读 · 0 评论 -
Leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15,7], ...原创 2018-04-26 15:48:44 · 103 阅读 · 0 评论 -
Leetcode 236. Lowest Common Ancestor of a Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2019-05-17 15:58:31 · 172 阅读 · 0 评论