左神算法进阶班5_2二叉树的最远距离

【题目】

二叉树中,一个节点可以往上走和往下走,那么从节点A总能走到节点B。

节点A走到节点B的距离为:A走到B最短路径【每个节点只走一次,不能重复走】上的节点个数。

求一棵二叉树上的最远距离

即,A到B可能有多条路径,求最长路径

【题解】

使用递归,求每一个节点为头的整棵子树的最大距离,则答案在其中

可能性一:

就是左子树的最大距离

可能性二:

就是右子树的最大距离

可能性三:

从左子树到右子树的最大距离

【代码】

  

  1 #pragma once
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <vector>
  5 
  6 using namespace std;
  7 
  8 struct Node
  9 {
 10     int val;
 11     Node* l;
 12     Node* r;
 13     Node(int a) :val(a), l(nullptr), r(nullptr) {}
 14 };
 15 
 16 struct returnType
 17 {
 18     int h;
 19     int maxDis;
 20     returnType(int a, int b) :h(a), maxDis(b) {}
 21 };
 22 
 23 //第一种递归
 24 returnType* findMaxDis1(Node* head)
 25 {
 26     if (head == nullptr)
 27         return new returnType(0, 0);
 28     //递归获得左右子树的大小
 29     returnType *lres, *rres;
 30     lres = findMaxDis1(head->l);
 31     rres = findMaxDis1(head->r);
 32 
 33     //判断
 34     int maxDis = max(max(lres->maxDis, rres->maxDis), lres->h + 1 + rres->h);//根据树的深度进行判断
 35     int h = max(lres->h, rres->h) + 1;//越向下深入探索,则树的深度加1
 36     return new returnType(h, maxDis);
 37 }
 38 
 39 
 40 //第二种递归
 41 vector<int> findMaxDis2(Node* head)
 42 {
 43     if (head == nullptr)
 44         return { 0, 0 };
 45 
 46     vector<int>lv, rv;
 47     lv = findMaxDis2(head->l);
 48     rv = findMaxDis2(head->r);
 49     int maxDis = max(max(lv[1], rv[1]), lv[0] + 1 + rv[0]);
 50     int h = max(lv[0], lv[0]) + 1;
 51     return { h,maxDis };
 52 }
 53 
 54 
 55 void Test()
 56 {
 57 
 58     Node* root = new Node(9);
 59     root->l = new Node(8);
 60     root->r = new Node(1);
 61     root->l->l = new Node(5);
 62     root->l->r = new Node(9);
 63     root->l->l->l = new Node(4);
 64     root->l->l->r = new Node(6);
 65     root->r->l = new Node(5);
 66     root->r->r = new Node(3);
 67     returnType* p = findMaxDis1(root);
 68     cout << p->maxDis << endl;
 69 
 70     root = nullptr;
 71     root = new Node(5);
 72     root->l = new Node(2);
 73     root->r = new Node(6);
 74     root->l->l = new Node(1);
 75     root->l->r = new Node(3);
 76     p = findMaxDis1(root);
 77     cout << p->maxDis << endl;
 78 
 79     vector<int>v;
 80     root = nullptr;
 81     root = new Node(9);
 82     root->l = new Node(8);
 83     root->r = new Node(1);
 84     root->l->l = new Node(5);
 85     root->l->r = new Node(9);
 86     root->l->l->l = new Node(4);
 87     root->l->l->r = new Node(6);
 88     root->r->l = new Node(5);
 89     root->r->r = new Node(3);
 90     v = findMaxDis2(root);
 91     cout << v[1] << endl;
 92 
 93     root = nullptr;
 94     root = new Node(5);
 95     root->l = new Node(2);
 96     root->r = new Node(6);
 97     root->l->l = new Node(1);
 98     root->l->r = new Node(3);
 99     v = findMaxDis2(root);
100     cout << v[1] << endl;
101 
102     
103 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11073086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
构建二叉树算法可以使用递归或迭代的方式实现。 以下是使用递归方式构建二叉树的示例代码: ``` class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class BinaryTree { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || inorder == null || preorder.length != inorder.length) { return null; } return buildTreeHelper(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); } private TreeNode buildTreeHelper(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) { if (preStart > preEnd || inStart > inEnd) { return null; } int rootVal = preorder[preStart]; int rootIndex = 0; for (int i = inStart; i <= inEnd; i++) { if (inorder[i] == rootVal) { rootIndex = i; break; } } int leftSize = rootIndex - inStart; TreeNode root = new TreeNode(rootVal); root.left = buildTreeHelper(preorder, inorder, preStart + 1, preStart + leftSize, inStart, rootIndex - 1); root.right = buildTreeHelper(preorder, inorder, preStart + leftSize + 1, preEnd, rootIndex + 1, inEnd); return root; } } ``` 以上代码中,`buildTree`方法接受两个数组,一个是先序遍历序列,一个是中序遍历序列。通过先序遍历序列的第一个节点确定根节点,在中序遍历序列中找到根节点的位置,根节点左边的节点为左子树,右边的节点为右子树。递归调用`buildTreeHelper`方法建立左右子树,并将其连接到根节点上。 以下是使用迭代方式构建二叉树的示例代码: ``` class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class BinaryTree { public TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || inorder == null || preorder.length != inorder.length) { return null; } Stack<TreeNode> stack = new Stack<>(); TreeNode root = new TreeNode(preorder[0]); stack.push(root); int inorderIndex = 0; for (int i = 1; i < preorder.length; i++) { TreeNode node = stack.peek(); if (node.val != inorder[inorderIndex]) { node.left = new TreeNode(preorder[i]); stack.push(node.left); } else { while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) { node = stack.pop(); inorderIndex++; } node.right = new TreeNode(preorder[i]); stack.push(node.right); } } return root; } } ``` 以上代码中,使用栈来辅助构建二叉树。先将先序遍历序列的第一个节点作为根节点入栈,然后依次遍历先序遍历序列中的每个节点,如果当前节点不等于中序遍历序列中的节点,则表示当前节点是根节点的左子树,将其作为左子节点入栈;否则,不断弹出栈顶元素,直到栈顶元素等于中序遍历序列中的节点,表示当前节点是某个节点的右子树,将其作为右子节点入栈。最终返回根节点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值