classSolution{ int maxvalue = Integer.MIN_VALUE; publicintmaxPathSum(TreeNode root){ dfs(root); return maxvalue; } publicintdfs(TreeNode node){ if (node == null) return0; int left = dfs(node.left); int right = dfs(node.right); left = left < 0 ? 0 : left; right = right < 0 ? 0 : right; maxvalue = Math.max(maxvalue, left + right + node.val); return Math.max(left, right) + node.val; } }
1 2 3 4 5 6
bottom up general steps: 1. Base case 2. 向子问题要答案 3. 利用子问题构建当前答案 4. 额外操作 5. 返回答案给父问题
Posted onInLeetcode
,
Tree Symbols count in article: 880Reading time ≈1 mins.
Question
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Posted onInLeetcode
,
Tree Symbols count in article: 1.4kReading time ≈1 mins.
Question
Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.
You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.
We want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.
Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
Example 2:
1 2 3
Input: root = [2,1,3] Output: [1,2,3]
Solution
Solution1 – recursive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ Node pre = null, head = null; public Node treeToDoublyList(Node root){ if (root == null) return root; inorderTraversal(root); head.left = pre; pre.right = head; return head; } privatevoidinorderTraversal(Node root){ if(root == null) return; inorderTraversal(root.left); //left if(head == null) head = root; if(pre != null) pre.right = root; //root root.left = pre; pre = root; inorderTraversal(root.right); //right } }
Posted onInLeetcode
,
Tree Symbols count in article: 1.1kReading time ≈1 mins.
Question
Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
Posted onInLeetcode
,
Tree Symbols count in article: 1.1kReading time ≈1 mins.
Question
Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
Posted onInLeetcode
,
Tree Symbols count in article: 1.2kReading time ≈1 mins.
Question
Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its root.
It is guaranteed that there is always possible to find a binary search tree with the given requirements for the given test cases.
A binary search tree is a binary tree where for every node, any descendant of Node.left has a value strictly less thanNode.val, and any descendant of Node.right has a value strictly greater thanNode.val.
A preorder traversal of a binary tree displays the value of the node first, then traverses Node.left, then traverses Node.right.
Posted onInLeetcode
,
Tree Symbols count in article: 1.6kReading time ≈1 mins.
Question
Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
// Encodes a tree to a single string. public String serialize(TreeNode root){ if (root == null) return""; String res = String.valueOf(root.val); if (root.left != null) res += "," + serialize(root.left); if (root.right != null) res += "," + serialize(root.right); return res; }
// Decodes your encoded data to tree. public TreeNode deserialize(String data){ if (data.isEmpty()) returnnull; Queue<String> q = new LinkedList<>(Arrays.asList(data.split(","))); return deserialize(q, Integer.MIN_VALUE, Integer.MAX_VALUE); } public TreeNode deserialize(Queue<String> q, int lower, int upper){ if (q.isEmpty()) returnnull; String s = q.peek(); int val = Integer.parseInt(s); if (val < lower || val > upper) returnnull; q.poll(); TreeNode root = new TreeNode(val); root.left = deserialize(q, lower, val); root.right = deserialize(q, val, upper); return root; } }