🌷二叉树
构造二叉树
题目:297. 二叉树的序列化与反序列化
算法:
朴素的想法前序遍历构造二叉树的序列,然后根据遍历的结果再构造二叉树。
按照前序遍历顺序进行序列化,反序列化的时候,就知道第一个元素是根节点的值,然后递归调用反序列化左右子树,接到根节点上即可,上述思路翻译成代码即可解决本题。
public class Codec {
String SEP = ",";
// Encodes a tree to a single string.
//前序遍历
void serialize(TreeNode root,StringBuilder sb){
if(root==null){
sb.append("#").append(SEP);
return;
}
sb.append(root.val).append(SEP);
serialize(root.left,sb);
serialize(root.right,sb);
}
public String serialize(TreeNode root) {
StringBuilder sb = new StringBuilder();
serialize(root,sb);
return sb.toString();
}
// Decodes your encoded data to tree.
TreeNode deserialize(LinkedList<String> nodes){
if(nodes.isEmpty()){
return null;
}
String first = nodes.removeFirst();
if(first.equals("#")) return null;
TreeNode root = new TreeNode(Integer.parseInt(first));
root.left = deserialize(nodes);
root.right = deserialize(nodes);
return root;
}
public TreeNode deserialize(String data) {
LinkedList<String> nodes = new LinkedList();
for(String s:data.split(SEP)){
nodes.addLast(s);
}
return deserialize(nodes);
}
}
124. 二叉树中的最大路径和
思路
最大路径和 取决于结点值和左右结点的单边路径值,在计算单边路径的时候同时计算最大路径和
//时间复杂度O(n)
//空间复杂度O(n) (递归调用栈最大等于二叉树树高)
class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if(root==null){
return 0;
}
OneSideMax(root);
return res;
}
int OneSideMax(TreeNode root){
if(root==null){
return 0;
}
int leftMax = Math.max(OneSideMax(root.left),0);
int rightMax = Math.max(OneSideMax(root.right),0);
//最大路径和 取决于结点值和左右结点的单边路径值
res = Math.max(res,leftMax+rightMax+root.val);
return root.val+Math.max(leftMax,rightMax); //返回结点的最大单边路径值
}
}
🌻二叉搜索树
题目:450. 删除二叉搜索树中的节点
算法:
如果 key > root.val,说明要删除的节点在右子树,root.right = deleteNode(root.right, key)。
如果 key < root.val,说明要删除的节点在左子树,root.left = deleteNode(root.left, key)。
如果 key == root.val,则该节点就是我们要删除的节点,则:
如果该节点是叶子节点,则直接删除它:root = null。
如果该节点不是叶子节点且有右节点,则用它的后继节点的值替代 root.val = successor.val,然后删除后继节点。
如果该节点不是叶子节点且只有左节点,则用它的前驱节点的值替代 root.val = predecessor.val,然后删除前驱节点。
返回 root
作者:LeetCode
链接:https://leetcode-cn.com/problems/delete-node-in-a-bst/solution/shan-chu-er-cha-sou-suo-shu-zhong-de-jie-dian-by-l/
//删除了这个节点用什么替代? 中序遍历的下一个 或者 上一个 画图理解
class Solution {
int pre(TreeNode node){
node = node.left;
while(node.right!=null){
node = node.right;
}
return node.val;
}
int next(TreeNode node){
node = node.right;
while(node.left!=null){
node = node.left;
}
return node.val;
}
public TreeNode deleteNode(TreeNode root, int key) {
if(root==null) return null;
if(key>root.val){
root.right = deleteNode(root.right,key);
}else if(key<root.val){
root.left = deleteNode(root.left,key);
}else{
//如果是叶子节点 不用替代直接删除
if(root.left==null && root.right==null){
root = null;
}else if(root.left!=null){
int preval = pre(root); //中序遍历的上一个
root.val = preval;
root.left = deleteNode(root.left,preval);
}else{
int nextval = next(root); //root.right!=null中序遍历的下一个
root.val = nextval;
root.right = deleteNode(root.right,nextval);
}
}
return root;
}
}
- 时间复杂度:O(H),H 值是树的高度,若树是一个平衡树则 H = logN。
- 空间复杂度:O(H),递归时堆栈使用的空间,H 是树的高度
173. 二叉搜索树迭代器
中序遍历
迭代的中序遍历
class BSTIterator {
Deque<TreeNode> stack; //中序遍历 用栈模拟递归过程
TreeNode cur;
public BSTIterator(TreeNode root) {
stack = new ArrayDeque<>();
cur = root;
}
public int next() {
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
int ret = cur.val;
cur = cur.right;
return ret;
}
public boolean hasNext() {
return !stack.isEmpty() || cur!=null;
}
}