binary-search-tree

C Program to copy one binary search tree to another

若如初见. 提交于 2020-01-15 06:02:12
问题 So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i am getting some segmentation fault, also i am not so sure if the logic is right. Here is the whole program, please lemme know where is it going wrong in t_cpy() or please just fix it for me.. #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *rlink; struct node *llink; }

Why won't my solution work to find minimum depth of a binary tree?

只愿长相守 提交于 2020-01-14 19:34:09
问题 I don't understand how my solution for finding minimum depth of a binary tree doesn't work? What am I doing wrong? Here's a link to the problem if you're curious: https://leetcode.com/problems/minimum-depth-of-binary-tree/submissions/ public int minDepth(TreeNode root) { if(root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); int ans = Math.min(left, right) + 1; return ans; } 回答1: Your code will not work in the case only one side is null , like 3 / \ 20 /

Balanced binary tree python

 ̄綄美尐妖づ 提交于 2020-01-11 13:01:52
问题 # stack_depth is initialised to 0 def find_in_tree(node, find_condition, stack_depth): assert (stack_depth < max_stack_depth), 'Deeper than max depth' stack_depth += 1 result = [] if find_condition(node): result += [node] for child_node in node.children: result.extend(find_in_tree(child_node, find_condition, stack_depth)) return result I need help understanding this piece of code. The question i want to answer is The Python function above searches the contents of a balanced binary tree. If an

Balanced binary tree python

前提是你 提交于 2020-01-11 13:01:18
问题 # stack_depth is initialised to 0 def find_in_tree(node, find_condition, stack_depth): assert (stack_depth < max_stack_depth), 'Deeper than max depth' stack_depth += 1 result = [] if find_condition(node): result += [node] for child_node in node.children: result.extend(find_in_tree(child_node, find_condition, stack_depth)) return result I need help understanding this piece of code. The question i want to answer is The Python function above searches the contents of a balanced binary tree. If an

java binary search tree find parent

☆樱花仙子☆ 提交于 2020-01-07 08:45:01
问题 im working on a method to find the parent of anode. I start at the root and then go down the leaves as long as they are not null and not the node of the child. below is my code, its a little messy because im trying to test it to see whats going wrong. The tree that i have is 10 / \ 2 20 \ / \ 3 18 22 / 21 The x that is being passed in is 20 so 10 is the parent but when i run it 22 comes out as the parent. the while loop seems to not be working, is it the way ive written it? public Node<E>

java binary search tree find parent

▼魔方 西西 提交于 2020-01-07 08:44:47
问题 im working on a method to find the parent of anode. I start at the root and then go down the leaves as long as they are not null and not the node of the child. below is my code, its a little messy because im trying to test it to see whats going wrong. The tree that i have is 10 / \ 2 20 \ / \ 3 18 22 / 21 The x that is being passed in is 20 so 10 is the parent but when i run it 22 comes out as the parent. the while loop seems to not be working, is it the way ive written it? public Node<E>

java binary search tree find parent

北城以北 提交于 2020-01-07 08:44:21
问题 im working on a method to find the parent of anode. I start at the root and then go down the leaves as long as they are not null and not the node of the child. below is my code, its a little messy because im trying to test it to see whats going wrong. The tree that i have is 10 / \ 2 20 \ / \ 3 18 22 / 21 The x that is being passed in is 20 so 10 is the parent but when i run it 22 comes out as the parent. the while loop seems to not be working, is it the way ive written it? public Node<E>

java binary search tree find parent

旧街凉风 提交于 2020-01-07 08:44:21
问题 im working on a method to find the parent of anode. I start at the root and then go down the leaves as long as they are not null and not the node of the child. below is my code, its a little messy because im trying to test it to see whats going wrong. The tree that i have is 10 / \ 2 20 \ / \ 3 18 22 / 21 The x that is being passed in is 20 so 10 is the parent but when i run it 22 comes out as the parent. the while loop seems to not be working, is it the way ive written it? public Node<E>

Convert sorted array to binary search tree with minimal height

早过忘川 提交于 2020-01-06 19:50:36
问题 I want to convert a sorted integer array into a binary search tree. I have posted my code below. What I cannot picture is how the recursion actually works with the for loop as inserting. So if my array is [1,3,4, 5,8,10] I make 4, which is the mid of the array, become the root of my BST, then loop from the start of array and insert to the tree with root just created. My question is why the order of result inserted is not as the sorted given array? public TreeNode sortedArrayToBST(int[] A) {

Tree of inheritance of Git branches

Deadly 提交于 2020-01-06 07:08:51
问题 Is there any way to replicate an inheritance tree of Git branches in shell? For example, I have local branches b1 , b2 , ... , bN as listed by git branch . Example: ++ b7 <--+ b4 | b8 <--+ b5 +-+ b2 | | ++ b1 <---+ b3 | +-+ b6 So, in the above case I would like to have a variables representing branch names (or their hashes, doesn't matter). I know about binary search tree data structures in shell (at least one approach which is working nicely). The problem is how to get a proper left or right