742. Closest Leaf in a Binary Tree
来源:互联网 发布:新加坡莱佛士书院知乎 编辑:程序博客网 时间:2024/06/10 11:03
Given a binary tree where every node has a unique value, and a target key k, find the value of the closest leaf node to target k in the tree.Here, closest to a leaf means the least number of edges travelled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children.In the following examples, the input tree is represented in flattened form row by row. The actual root tree given will be a TreeNode object.Example 1:Input:root = [1, 3, 2], k = 1Diagram of binary tree: 1 / \ 3 2Output: 2 (or 3)Explanation: Either 2 or 3 is the closest leaf node to the target of 1.Example 2:Input:root = [1], k = 1Output: 1Explanation: The closest leaf node is the root node itself.Example 3:Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree: 1 / \ 2 3 / 4 / 5 / 6Output: 3Explanation: The leaf node with value 3 (and not the leaf node with value 6) is closest to the node with value 2.Note:root represents a binary tree with at least 1 node and at most 1000 nodes.Every node has a unique node.val in range [1, 1000].There exists some node in the given binary tree for which node.val == k.
这道题目思路比较简单,将目标节点的路径求出来,然后求每个叶子节点到该目标节点的距离,选取距离最近的节点即可。、
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int min = 1000; int val = 0; bool findTargetNode(TreeNode * root,int key,vector<int> &distance){ if(root == NULL){ return false; } if(root->val == key){ return true; } if(root->left){ distance.push_back(0); if(findTargetNode(root->left,key,distance)){ return true; } distance.pop_back(); } if(root->right){ distance.push_back(1); if(findTargetNode(root->right,key,distance)){ return true; } distance.pop_back(); } return false; } int calculateDistance(vector<int> &path,vector<int> &distance){ int i = 0; for(i = 0;i < path.size()&&i < distance.size();++i){ if(path[i] != distance[i]){ break; } } return path.size() + distance.size()-2*i; } bool findShortDistance(TreeNode * root,vector<int> & path,vector<int> & distance){ if(root == NULL){ return false; } /*this node is leaf node*/ if(root->left == NULL&&root->right == NULL){ int dis = calculateDistance(path,distance); if(dis<min){ min = dis; val = root->val; } } if(root->left){ distance.push_back(0); findShortDistance(root->left,path,distance); distance.pop_back(); } if(root->right){ distance.push_back(1); findShortDistance(root->right,path,distance); distance.pop_back(); } return true; } int findClosestLeaf(TreeNode* root, int k) { vector<int> path; vector<int> distance; /*initial*/ findTargetNode(root,k,path); /*debug*/ for(int i = 0; i < path.size();++i){ cout<<path[i]<<endl; } if(root == NULL){ return 0; } findShortDistance(root,path,distance); return this->val; }};
阅读全文
0 0
- 742. Closest Leaf in a Binary Tree
- 742. Closest Leaf in a Binary Tree
- LWC 62:742. Closest Leaf in a Binary Tree
- 742. Closest Leaf in a Binary Tree的思路
- leetcode_742. Closest Leaf in a Binary Tree ? 待解决
- LeetCode | 742. Closest Leaf in a Binary Tree12_16
- Closest Value in Binary Search Tree
- 【Leetcode】Path Sum from root to leaf in binary tree
- 【LeetCode】Binary Tree Leaf Sum
- Closest Binary Search Tree Value
- Closest Binary Search Tree Value
- Vertical Sum in a given Binary Tree
- Least Common Ancestor in a Binary Tree
- Largest BST in a Binary Tree
- Random Node in A Binary Tree
- Lowest Common Ancestor in a Binary Tree
- Insert Node in a Binary Search Tree
- Reverse a binary tree in O(1)
- java学习第三天
- python自学笔记3
- linux汇编学习(6)---添加根文件系统
- Java学习之训练---求数组的交集、并集以及只在A数组中出现的子串集合
- XML文件作为数据库
- 742. Closest Leaf in a Binary Tree
- 指针与指针变量
- vue实现app问题总结(一)vue-cli本地开发数据Mock
- Ubuntu 14.04快速搭建SVN服务器及日常使用
- 要整理的内容
- 交换机(一)
- 线性神经网络
- 【TensorFlow】优化方法optimizer总结(SGD,Adagrad,Adadelta,Adam,Adamax,Nadam)解析(十三)
- squid介绍及其简单配置