随笔分类 - Tree
easy
摘要:Solution1:广度优先 class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); List<List<Integer
阅读全文
摘要:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l
阅读全文
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Solution1: class Solution { public boolean isSymmetric(
阅读全文
摘要: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 a
阅读全文
摘要:Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Solution 1://先遍历左节点然后把左节点设为
阅读全文
摘要:Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the roo
阅读全文
摘要: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
阅读全文
摘要://Solution 1:递归 public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list =new ArrayList<>(); addNode(list, root); return list;}privat
阅读全文
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST'
阅读全文
摘要:Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should
阅读全文
摘要:Solution 1: public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> wrapList = new Link
阅读全文
摘要:Solution 1: public class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> sol = new ArrayList<>(); travel(r
阅读全文
摘要:Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 见剑指offer重建二叉树
阅读全文
摘要:Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. class Soluti
阅读全文
摘要:given a binary tree, return the preorder traversal of its nodes' values. Solution 1://非递归 Solution 1://非递归 public class Solution { public List<Integer
阅读全文
摘要:Given a binary tree, return the postorder traversal of its nodes' values. Solution 1: class Solution { public List<Integer> postorderTraversal(TreeNod
阅读全文
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. class Solution { public TreeNode sortedL
阅读全文
摘要://例如根节点为1,左2右3 class Solution { TreeNode prev = null; public void flatten(TreeNode root) {//先把最大的数设在root.right,然后剩下的数一个个往里加 if (root == null) return;
阅读全文
摘要:Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL. Initially, all next
阅读全文
摘要://按层从左往右检索 //按层从左往右检索 //按层从左往右检索 //按层从左往右检索 public class Solution { public void connect(TreeLinkNode root) { while (root != null) { TreeLinkNode tempC
阅读全文