Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >2022-05-22:给定一个二叉树,找到最近公共祖先。rust代码修改

2022-05-22:给定一个二叉树,找到最近公共祖先。rust代码修改

原创
作者头像
福大大架构师每日一题
发布于 2022-05-23 13:12:13
发布于 2022-05-23 13:12:13
3390
举报

2022-05-22:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。

如何时间复杂度O(N),额外空间复杂度O(1),解决最低公共祖先问题?

力扣236。二叉树的最近公共祖先。

答案2022-05-23:

莫里斯遍历。主要是修改rust代码。

rust代码修改如下:

代码语言:rust
AI代码解释
复制
use std::cell::RefCell;
use std::rc::Rc;

fn main() {
    let mut head = Some(Rc::new(RefCell::new(TreeNode::new(1))));
    let mut left_left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
    let mut left_right = Some(Rc::new(RefCell::new(TreeNode::new(5))));
    head.as_ref().unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(2))));
    head.as_ref().unwrap().borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(3))));
    head.as_ref()
        .unwrap()
        .borrow()
        .left
        .as_ref()
        .unwrap()
        .borrow_mut()
        .left = Some(Rc::clone(&left_left.as_ref().unwrap()));
    head.as_ref()
        .unwrap()
        .borrow()
        .left
        .as_ref()
        .unwrap()
        .borrow_mut()
        .right = Some(Rc::clone(&left_right.as_ref().unwrap()));
    let ans = Solution::lowest_common_ancestor(
        Some(Rc::clone(&head.as_ref().unwrap())),
        Some(Rc::clone(&left_left.as_ref().unwrap())),
        Some(Rc::clone(&left_right.as_ref().unwrap())),
    );
    if ans.is_none() {
        println!("ans = 空");
    } else {
        println!("ans val = {}", ans.as_ref().unwrap().borrow().val);
    }
    println!("-----------------");
    println!("head = {}", head.as_ref().unwrap().borrow().val);
    println!(
        "head.left = {}",
        head.as_ref()
            .unwrap()
            .borrow()
            .left
            .as_ref()
            .unwrap()
            .borrow()
            .val
    );
    println!(
        "head.right = {}",
        head.as_ref()
            .unwrap()
            .borrow()
            .right
            .as_ref()
            .unwrap()
            .borrow()
            .val
    );
    println!(
        "head.left.left = {}",
        head.as_ref()
            .unwrap()
            .borrow()
            .left
            .as_ref()
            .unwrap()
            .borrow()
            .left
            .as_ref()
            .unwrap()
            .borrow()
            .val
    );
    println!(
        "head.left.right = {}",
        head.as_ref()
            .unwrap()
            .borrow()
            .left
            .as_ref()
            .unwrap()
            .borrow()
            .right
            .as_ref()
            .unwrap()
            .borrow()
            .val
    );
}

pub struct TreeNode {
    pub val: i32,
    pub left: Option<Rc<RefCell<TreeNode>>>,
    pub right: Option<Rc<RefCell<TreeNode>>>,
}

impl TreeNode {
    pub fn new(val: i32) -> Self {
        Self {
            val,
            left: None,
            right: None,
        }
    }
}

pub struct Solution {}

// 力扣里提交以下的代码
impl Solution {
    // 该方法亮点在于:时间复杂度O(N),额外空间复杂度O(1)
    pub fn lowest_common_ancestor(
        mut head: Option<Rc<RefCell<TreeNode>>>,
        mut o1: Option<Rc<RefCell<TreeNode>>>,
        mut o2: Option<Rc<RefCell<TreeNode>>>,
    ) -> Option<Rc<RefCell<TreeNode>>> {
        // if (findFirst(o1.left, o1, o2) != null || findFirst(o1.right, o1, o2) != null) {
        //     return o1;
        // }
        if !find_first(
            if o1.as_ref().unwrap().borrow().left.is_none() {
                None
            } else {
                Some(Rc::clone(
                    &o1.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                ))
            },
            if o1.is_none() {
                None
            } else {
                Some(Rc::clone(&o1.as_ref().unwrap()))
            },
            if o2.is_none() {
                None
            } else {
                Some(Rc::clone(&o2.as_ref().unwrap()))
            },
        )
        .is_none()
            || !find_first(
                if o1.as_ref().unwrap().borrow().right.is_none() {
                    None
                } else {
                    Some(Rc::clone(
                        &o1.as_ref().unwrap().borrow().right.as_ref().unwrap(),
                    ))
                },
                if o1.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o1.as_ref().unwrap()))
                },
                if o2.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o2.as_ref().unwrap()))
                },
            )
            .is_none()
        {
            return if o1.is_none() {
                None
            } else {
                Some(Rc::clone(&o1.as_ref().unwrap()))
            };
        }

        // if (findFirst(o2.left, o1, o2) != null || findFirst(o2.right, o1, o2) != null) {
        // 	return o2;
        // }
        if !find_first(
            if o2.as_ref().unwrap().borrow().left.is_none() {
                None
            } else {
                Some(Rc::clone(
                    &o2.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                ))
            },
            if o1.is_none() {
                None
            } else {
                Some(Rc::clone(&o1.as_ref().unwrap()))
            },
            if o2.is_none() {
                None
            } else {
                Some(Rc::clone(&o2.as_ref().unwrap()))
            },
        )
        .is_none()
            || !find_first(
                if o2.as_ref().unwrap().borrow().right.is_none() {
                    None
                } else {
                    Some(Rc::clone(
                        &o2.as_ref().unwrap().borrow().right.as_ref().unwrap(),
                    ))
                },
                if o1.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o1.as_ref().unwrap()))
                },
                if o2.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o2.as_ref().unwrap()))
                },
            )
            .is_none()
        {
            return if o2.is_none() {
                None
            } else {
                Some(Rc::clone(&o2.as_ref().unwrap()))
            };
        }

        //left_aim := findFirst(head, o1, o2)
        let mut left_aim = find_first(
            if head.is_none() {
                None
            } else {
                Some(Rc::clone(&head.as_ref().unwrap()))
            },
            if o1.is_none() {
                None
            } else {
                Some(Rc::clone(&o1.as_ref().unwrap()))
            },
            if o2.is_none() {
                None
            } else {
                Some(Rc::clone(&o2.as_ref().unwrap()))
            },
        );
        // TreeNode cur = head;
        let mut cur = if head.is_none() {
            None
        } else {
            Some(Rc::clone(&head.as_ref().unwrap()))
        };
        // TreeNode most_right = null;
        let mut most_right: Option<Rc<RefCell<TreeNode>>> = None;
        // TreeNode ans = null;
        let mut ans: Option<Rc<RefCell<TreeNode>>> = None;
        //for cur != nil {
        while !cur.is_none() {
            // 	most_right = cur.left;
            most_right = if cur.as_ref().unwrap().borrow().left.is_none() {
                None
            } else {
                Some(Rc::clone(
                    &cur.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                ))
            };

            //if most_right != nil {
            if !most_right.is_none() {
                //while most_right.right != null && most_right.right != cur {
                while !most_right.as_ref().unwrap().borrow().right.is_none()
                    && !is_eq(
                        &Some(Rc::clone(
                            &most_right
                                .as_ref()
                                .unwrap()
                                .borrow()
                                .right
                                .as_ref()
                                .unwrap(),
                        )),
                        &cur,
                    )
                {
                    // 			most_right = most_right.right;
                    most_right = if most_right.as_ref().unwrap().borrow().right.is_none() {
                        None
                    } else {
                        Some(Rc::clone(
                            &most_right
                                .as_ref()
                                .unwrap()
                                .borrow()
                                .right
                                .as_ref()
                                .unwrap(),
                        ))
                    };
                }

                //if (most_right.right == null) {
                if most_right.as_ref().unwrap().borrow().right.is_none() {
                    // 			most_right.right = cur;
                    most_right.as_ref().unwrap().borrow_mut().right = if cur.is_none() {
                        None
                    } else {
                        Some(Rc::clone(&cur.as_ref().unwrap()))
                    };
                    // 			cur = cur.left;
                    cur = if cur.as_ref().unwrap().borrow().left.is_none() {
                        None
                    } else {
                        Some(Rc::clone(
                            &cur.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                        ))
                    };
                    continue;
                } else {
                    //most_right.right = null;
                    most_right.as_ref().unwrap().borrow_mut().right = None;
                    //if (findLeftAim(cur.left, left_aim)) {
                    if find_left_aim(
                        if cur.as_ref().unwrap().borrow().left.is_none() {
                            None
                        } else {
                            Some(Rc::clone(
                                &cur.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                            ))
                        },
                        if left_aim.is_none() {
                            None
                        } else {
                            Some(Rc::clone(&left_aim.as_ref().unwrap()))
                        },
                    ) {
                        //if (ans == null && findFirst(left_aim.right, o1, o2) != null) {
                        if ans.is_none()
                            && !find_first(
                                if left_aim.as_ref().unwrap().borrow().right.is_none() {
                                    None
                                } else {
                                    Some(Rc::clone(
                                        &left_aim
                                            .as_ref()
                                            .unwrap()
                                            .borrow()
                                            .right
                                            .as_ref()
                                            .unwrap(),
                                    ))
                                },
                                if o1.is_none() {
                                    None
                                } else {
                                    Some(Rc::clone(&o1.as_ref().unwrap()))
                                },
                                if o2.is_none() {
                                    None
                                } else {
                                    Some(Rc::clone(&o2.as_ref().unwrap()))
                                },
                            )
                            .is_none()
                        {
                            //ans = left_aim;
                            ans = if left_aim.is_none() {
                                None
                            } else {
                                Some(Rc::clone(&left_aim.as_ref().unwrap()))
                            };
                        }

                        // left_aim = cur;
                        left_aim = if cur.is_none() {
                            None
                        } else {
                            Some(Rc::clone(&cur.as_ref().unwrap()))
                        };
                    }
                }
            }
            // 	cur = cur.right;
            cur = if cur.as_ref().unwrap().borrow().right.is_none() {
                None
            } else {
                Some(Rc::clone(
                    &cur.as_ref().unwrap().borrow().right.as_ref().unwrap(),
                ))
            };
        }

        if !ans.is_none() {
            return ans;
        } else {
            if !find_first(
                if left_aim.as_ref().unwrap().borrow().right.is_none() {
                    None
                } else {
                    Some(Rc::clone(
                        &left_aim.as_ref().unwrap().borrow().right.as_ref().unwrap(),
                    ))
                },
                if o1.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o1.as_ref().unwrap()))
                },
                if o2.is_none() {
                    None
                } else {
                    Some(Rc::clone(&o2.as_ref().unwrap()))
                },
            )
            .is_none()
            {
                return Some(Rc::clone(&left_aim.as_ref().unwrap()));
            } else {
                return Some(Rc::clone(&head.as_ref().unwrap()));
            }
        }
    }
}

fn find_left_aim(
    mut head: Option<Rc<RefCell<TreeNode>>>,
    mut left_aim: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
    let mut tail = reverse_edge(Some(Rc::clone(&head.as_ref().unwrap())));
    //TreeNode cur = tail;
    let mut cur = if tail.is_none() {
        None
    } else {
        Some(Rc::clone(&tail.as_ref().unwrap()))
    };
    // boolean ans = false;
    let mut ans = false;
    while !cur.is_none() {
        if is_eq(&cur, &left_aim) {
            ans = true;
        }
        // 	cur = cur.right;
        cur = if cur.as_ref().unwrap().borrow().right.is_none() {
            None
        } else {
            Some(Rc::clone(
                &cur.as_ref().unwrap().borrow().right.as_ref().unwrap(),
            ))
        };
    }
    reverse_edge(Some(Rc::clone(&tail.as_ref().unwrap())));
    return ans;
}

fn reverse_edge(mut from: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
    //TreeNode pre = null;
    let mut pre: Option<Rc<RefCell<TreeNode>>> = None;
    //TreeNode next = null;
    let mut next: Option<Rc<RefCell<TreeNode>>> = None;
    while !is_eq(&from, &None) {
        // next = from.right;
        next = if from.as_ref().unwrap().borrow().right.is_none() {
            None
        } else {
            Some(Rc::clone(
                &from.as_ref().unwrap().borrow().right.as_ref().unwrap(),
            ))
        };
        // from.right = pre;
        from.as_ref().unwrap().borrow_mut().right = if pre.is_none() {
            None
        } else {
            Some(Rc::clone(&pre.as_ref().unwrap()))
        };
        // pre = from;
        pre = if from.is_none() {
            None
        } else {
            Some(Rc::clone(&from.as_ref().unwrap()))
        };
        // from = next;
        from = if next.is_none() {
            None
        } else {
            Some(Rc::clone(&next.as_ref().unwrap()))
        };
    }
    return pre;
}

fn find_first(
    mut head: Option<Rc<RefCell<TreeNode>>>,
    mut o1: Option<Rc<RefCell<TreeNode>>>,
    mut o2: Option<Rc<RefCell<TreeNode>>>,
) -> Option<Rc<RefCell<TreeNode>>> {
    //if head == nil {
    if head.is_none() {
        return None;
    }
    //cur := head
    let mut cur: Option<Rc<RefCell<TreeNode>>> = Some(Rc::clone(&head.as_ref().unwrap()));
    //var most_right *TreeNode
    let mut most_right: Option<Rc<RefCell<TreeNode>>> = None;
    //var first *TreeNode
    let mut first: Option<Rc<RefCell<TreeNode>>> = None;
    //for cur != nil {
    while !cur.is_none() {
        //most_right = cur.left;
        most_right = if cur.as_ref().unwrap().borrow().left.is_none() {
            None
        } else {
            Some(Rc::clone(
                &cur.as_ref().unwrap().borrow().left.as_ref().unwrap(),
            ))
        };

        //if most_right != nil {
        if !most_right.is_none() {
            while !most_right.as_ref().unwrap().borrow().right.is_none()
                && !is_eq(&most_right.as_ref().unwrap().borrow().right, &cur)
            {
                //most_right = most_right.right;
                most_right = if most_right.as_ref().unwrap().borrow().right.is_none() {
                    None
                } else {
                    Some(Rc::clone(
                        &most_right
                            .as_ref()
                            .unwrap()
                            .borrow()
                            .right
                            .as_ref()
                            .unwrap(),
                    ))
                };
            }
            if is_eq(&most_right.as_ref().unwrap().borrow().right, &None) {
                if is_eq(&first, &None) && (is_eq(&cur, &o1) || is_eq(&cur, &o2)) {
                    //             first = cur;
                    first = if cur.is_none() {
                        None
                    } else {
                        Some(Rc::clone(&cur.as_ref().unwrap()))
                    };
                }
                //         most_right.right = cur;
                most_right.as_ref().unwrap().borrow_mut().right = if cur.is_none() {
                    None
                } else {
                    Some(Rc::clone(&cur.as_ref().unwrap()))
                };
                //         cur = cur.left;
                cur = if cur.as_ref().unwrap().borrow().left.is_none() {
                    None
                } else {
                    Some(Rc::clone(
                        &cur.as_ref().unwrap().borrow().left.as_ref().unwrap(),
                    ))
                };
                continue;
            } else {
                //most_right.right = nil;
                most_right.as_ref().unwrap().borrow_mut().right = None;
            }
        } else {
            //if first == nil && (cur == o1 || cur == o2) {
            if is_eq(&first, &None) && (is_eq(&cur, &o1) || is_eq(&cur, &o2)) {
                //         first = cur;
                first = if cur.as_ref().is_none() {
                    None
                } else {
                    Some(Rc::clone(&cur.as_ref().unwrap()))
                };
            }
        }
        // cur = cur.right;
        cur = if cur.as_ref().unwrap().borrow().right.is_none() {
            None
        } else {
            Some(Rc::clone(
                &cur.as_ref().unwrap().borrow().right.as_ref().unwrap(),
            ))
        };
    }
    return first;
}

fn is_eq(o1: &Option<Rc<RefCell<TreeNode>>>, o2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
    if o1.is_none() && o2.is_none() {
        return true;
    }
    if o1.is_none() || o2.is_none() {
        return false;
    }
    return o1.as_ref().unwrap().borrow().val == o2.as_ref().unwrap().borrow().val;
}

执行结果如下:

在这里插入图片描述
在这里插入图片描述

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
2022-05-22:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。
福大大架构师每日一题
2022/05/22
4750
2022-05-22:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
2022-10-09:我们给出了一个(轴对齐的)二维矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其
2022-10-09:我们给出了一个(轴对齐的)二维矩形列表 rectangles 。
福大大架构师每日一题
2022/10/09
3150
2022-10-09:我们给出了一个(轴对齐的)二维矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其
2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉树中,存在一条一直向下的路径 且每个点的数值恰好一一对应
2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表
福大大架构师每日一题
2023/05/10
4570
2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉树中,存在一条一直向下的路径 且每个点的数值恰好一一对应
rust 入门笔记:使用rust实现双向链表、二叉树
github地址:https://github.com/yunwei37/os-summer-of-code-daily
云微
2023/02/11
6900
【Rust日报】2023-07-05 让我们从 abandon 开始--用 rust 写链表
虽然 Rust 的标准库中已经有了一个LinkedList数据结构,但创建自己的数据结构是了解更多 Rust 的一种有趣的方式。
MikeLoveRust
2023/09/26
2390
【Rust日报】2023-07-05 让我们从 abandon 开始--用 rust 写链表
阿里巴巴的算法面试题JAVA,python,go,rust ,js,C++,Swift,Kotlin,Scala解法大全
算法思路相同,都是使用dummy节点和cur指针,两两交换链表节点,并返回dummy.next作为结果。
疯狂的KK
2023/05/16
1K0
阿里巴巴的算法面试题JAVA,python,go,rust ,js,C++,Swift,Kotlin,Scala解法大全
2023-06-14:我们从二叉树的根节点 root 开始进行深度优先搜索。 在遍历中的每个节点处,我们输出 D 条短划线(其中
2.定义一个结构体类型 TreeNode,表示二叉树的节点,包括节点值 Val,左子节点 Left,右子节点 Right。
福大大架构师每日一题
2023/06/21
2790
2023-06-14:我们从二叉树的根节点 root 开始进行深度优先搜索。 在遍历中的每个节点处,我们输出 D 条短划线(其中
JavaScript算法题总结 (三)二叉树
BM23 二叉树的前序遍历 /* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ function preorderTraversal( root )
henu_Newxc03
2022/05/12
2610
JavaScript算法题总结 (三)二叉树
golang刷leetcode 二叉树(10)最近公共祖先
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
golangLeetcode
2022/08/02
2360
golang刷leetcode 二叉树(10)最近公共祖先
2021-10-06:二叉树的锯齿形层序遍历。给定一个二叉树,返
2021-10-06:二叉树的锯齿形层序遍历。给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。力扣103。
福大大架构师每日一题
2021/10/06
1950
Rust 关联常量,泛型结构体,内部可变性
Rust 在其类型系统中的另一个特性也采用了类似于 C# 和 Java 的思想,有些值是与类型而不是该类型的特定实例关联起来的。在 Rust 中,这些叫作关联常量。
草帽lufei
2024/05/08
2890
Rust 关联常量,泛型结构体,内部可变性
2022-03-19:已知一棵二叉树上所有的值都不一样, 给定这棵二叉树的头节点head, 给定一个整型数组arr,arr里放着不同的值,每个值一定在树上 返回
2022-03-19:已知一棵二叉树上所有的值都不一样, 给定这棵二叉树的头节点head, 给定一个整型数组arr,arr里放着不同的值,每个值一定在树上 返回数组里所有值的最低公共祖先。 答案2022-03-19: 递归。 代码用golang编写。代码如下: package main import "fmt" func main() { root := &TreeNode{val: 1} root.left = &TreeNode{val: 2} root.right = &TreeNode{v
福大大架构师每日一题
2022/03/19
5500
二叉树高频题
Given a binary tree, return the preorder traversal of its nodes' values.
王脸小
2019/10/24
7490
二叉树篇二刷总结
二叉树篇,我们总共做了有关二叉树的遍历方式、求解二叉树的属性、对二叉树的修改以及构造等这几类的题型, 总结下来就是对二叉树的各种遍历方式的不同程度应用。
用户11097514
2024/05/31
1170
二叉树篇二刷总结
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
时间复杂度 O(N) : 其中 N 为二叉树节点数;最差情况下,需要递归遍历树的所有节点。
福大大架构师每日一题
2020/08/30
4380
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
【Leetcode】二叉树的最近公共祖先,二叉搜索树转换成排好序的双向链表,前序遍历与中序遍历构造二叉树
观察上图,节点1和节点4的最近公共祖先是3,这是不是很像相交链表的问题,关于相交链表,曾经我在另一篇文章里写到过,读者可以参考:反转链表 合并链表 相交链表
aosei
2024/01/23
1950
【Leetcode】二叉树的最近公共祖先,二叉搜索树转换成排好序的双向链表,前序遍历与中序遍历构造二叉树
【二叉树进阶】二叉树经典面试题——最近公共祖先问题
7和4呢,2 、5 、3是不是都是它们两个的公共祖先啊,但是题目要求找最近的公共祖先,所以是2。 再看一种情况
YIN_尹
2024/01/23
1640
【二叉树进阶】二叉树经典面试题——最近公共祖先问题
2021-10-08:填充每个节点的下一个右侧节点指针。给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节
2021-10-08:填充每个节点的下一个右侧节点指针。给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。进阶:你只能使用常量级额外空间。使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。力扣116。
福大大架构师每日一题
2021/10/14
6470
听GPT 讲Rust源代码--library/core/src(6)
在Rust源代码中,rust/library/core/src/num/dec2flt/common.rs的作用是定义了一些用于十进制到浮点数转化的共享逻辑。以下是对该文件内容的详细介绍:
fliter
2023/11/18
2960
听GPT 讲Rust源代码--library/core/src(6)
LeetCode 863. 二叉树中所有距离为 K 的结点(公共祖先/ DFS+BFS)
给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K 。
Michael阿明
2020/07/13
8650
LeetCode 863. 二叉树中所有距离为 K 的结点(公共祖先/ DFS+BFS)
推荐阅读
2022-05-22:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
4750
2022-10-09:我们给出了一个(轴对齐的)二维矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其
3150
2023-05-10:给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表 如果在二叉树中,存在一条一直向下的路径 且每个点的数值恰好一一对应
4570
rust 入门笔记:使用rust实现双向链表、二叉树
6900
【Rust日报】2023-07-05 让我们从 abandon 开始--用 rust 写链表
2390
阿里巴巴的算法面试题JAVA,python,go,rust ,js,C++,Swift,Kotlin,Scala解法大全
1K0
2023-06-14:我们从二叉树的根节点 root 开始进行深度优先搜索。 在遍历中的每个节点处,我们输出 D 条短划线(其中
2790
JavaScript算法题总结 (三)二叉树
2610
golang刷leetcode 二叉树(10)最近公共祖先
2360
2021-10-06:二叉树的锯齿形层序遍历。给定一个二叉树,返
1950
Rust 关联常量,泛型结构体,内部可变性
2890
2022-03-19:已知一棵二叉树上所有的值都不一样, 给定这棵二叉树的头节点head, 给定一个整型数组arr,arr里放着不同的值,每个值一定在树上 返回
5500
二叉树高频题
7490
二叉树篇二刷总结
1170
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
4380
【Leetcode】二叉树的最近公共祖先,二叉搜索树转换成排好序的双向链表,前序遍历与中序遍历构造二叉树
1950
【二叉树进阶】二叉树经典面试题——最近公共祖先问题
1640
2021-10-08:填充每个节点的下一个右侧节点指针。给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节
6470
听GPT 讲Rust源代码--library/core/src(6)
2960
LeetCode 863. 二叉树中所有距离为 K 的结点(公共祖先/ DFS+BFS)
8650
相关推荐
2022-05-22:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档