首页 > 数据库 >[Oracle] LeetCode 1740 Find Distance in a Binary Tree

[Oracle] LeetCode 1740 Find Distance in a Binary Tree

时间:2022-08-19 03:22:06浏览次数:74  
标签:rt Distance right TreeNode Binary int Tree return left

Given the root of a binary tree and two integers p and q, return the distance between the nodes of value p and value q in the tree.

The distance between two nodes is the number of edges on the path from one to the other.

Solution

求树上两个点之间的距离。很经典的做法就是求出 \(LCA\),然后分别求出两点分别到 \(LCA\) 的距离即可

点击查看代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    TreeNode* LCA(TreeNode* rt, int p, int q){
        if(!rt) return NULL;
        if(rt->val==p || rt->val==q)return rt;
        TreeNode* leftrt = LCA(rt->left, p,q);
        TreeNode* rightrt = LCA(rt->right, p, q);
        if(leftrt&&rightrt)return rt;
        if(!leftrt && !rightrt)return NULL;
        return leftrt?leftrt:rightrt;
    }
    
    int get_depth(TreeNode* rt, int v, int dep){
        if(!rt) return 0;
        if(rt->val==v) return dep;
        return get_depth(rt->right, v, dep+1)+get_depth(rt->left, v, dep+1);
    }
    
public:
    int findDistance(TreeNode* root, int p, int q) {
        TreeNode* lca = LCA(root, p, q);
        return get_depth(lca,p,0)+get_depth(lca,q,0);
    }
};

标签:rt,Distance,right,TreeNode,Binary,int,Tree,return,left
From: https://www.cnblogs.com/xinyu04/p/16600690.html

相关文章

  • PAT Advanced 1020 Tree Traversals(25)
    题目描述:Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Giventhepostorderandinordertraversalsequences,youaresupposedtoou......
  • [Oracle] LeetCode 333 Largest BST Subtree
    Giventherootofabinarytree,findthelargestsubtree,whichisalsoaBinarySearchTree(BST),wherethelargestmeanssubtreehasthelargestnumberof......
  • 145.binary-tree-postorder-traversal 二叉树的后序遍历
    对比前序遍历的"中左右",后序遍历是"左右中",颠倒一下就是"中右左",所以可以参照前序遍历的迭代法来写迭代遍历。#include<algorithm>#include<stack>#include<vector>......
  • 144.binary-tree-preorder-traversal 二叉树的前序遍历
    前序遍历即中左右,前中后序遍历区别就在于中节点是在前、中还是后。利用栈实现二叉树的迭代遍历:#include<stack>#include<vector>usingstd::stack;usingstd::vecto......
  • Fisher Information and Bures distance
    \[F=Tr\sqrt{\sqrt{\rho_{\theta}}\left(\rho_{\theta}+\partial\rho_{\theta}d\theta+\partial^2\rho_{\theta}\frac{d\theta^2}{2}\right)\sqrt{\rho_{\thet......
  • Dsu on tree
    Dsuontree代指树上启发式合并,并非是并查集个人觉得这个算法的思想跟莫队有些许相似,但是又利用了树链剖分的一些性质,从而使得复杂度大大降低,优秀的o(nlgn)。需要的前......
  • AVL Tree (1) - Definition, find and Rotation
    1.定义(15-1)[AVLtree]:一棵空二叉树是AVLtree;若T是一棵非空二叉树,则T满足以下两个条件时,T是一棵AVLtree:T_LeftSubtree和T_RightSubtree是AV......
  • binary search tree(bst)
    什么是bst?bst树,通常称为二叉搜索树,又叫二叉排序树,bst是一种特殊的二叉树结构,也是一种常见的数据结构类型,其中,bst很明显的特性是根节点大于左子树的节点小于右子树的节点,......
  • CF715C Digit Tree
    沝黑。首先这种统计路径的问题一般联想点分治,然后考虑如何处理经过一个点\(u\)的路径。考虑有一个点\(p\inu\)的子树,然后记录路径\(p\tou\)和路径\(u\top\)的......
  • Balanced Tree (数学函数式子的处理)
    题目大意•求n个点组成的每个节点都满足左右子树大小相差至多1的二叉树个数.•0≤n<264.•关键词:计数2022-暑假-VirtualJudge(vjudge.net)思路:直接用......