#### Category : binary-tree

I was solving question of merging two binary trees in LeetCode using recursion but recursion is not working. But recursion is not working in my code.I want to know where I am making mistake. class Solution: def merge(self, root1, root2): if root1 != None and root2 != None: root1.val += root2.val if root1 == None ..

I am looking for implementing BFS (Breadth-First Search) of binary tree in Python using Stack (not Queue!). class Stack: def __init__(self): self.data = [] def Empty(self): return self.data == [] def Push(self, x): self.data.append(x) def Pop(self): return self.data.pop() def Peek(self): return self.data[len(self.data)-1] def Size(self): return len(self.data) class Node: def __init__(self, data, left, right): self.data = ..

def affiche(a): T = [a] if T == []: return 0 else: if T[0] == 1: T.append ("[",a,"]") return T print(affiche("item")) I have an assignment to build a binary tree in a print it in the format [[branch]root Node[branch]]. For example, ABC should be printed as [[B]A[C]]. My code doesn’t seem to work. Instead, it ..

I’m struggling to do this task and a friend adviced me to ask my problem here, can someone help me? tips to how to proceed Source: Python-3x..

from tree import Node, leaf from expr_parser import parse from typing import Optional def opt_times_two(e:Optional[Node])->Optional[Node]: if e.left != e.right: return None if e.mark == "+": if e.left == e.right: return #fill in assert opt_times_two(parse("x + y")) == None assert opt_times_two(parse("x + (x + x)")) == None assert opt_times_two(parse("x + x")) == parse("2 * x") So ..

from tree import Node, leaf def is_var(tree:Node)->bool: if type(tree.mark) == str: return True elif tree.mark == "+" or tree.mark == "*": return False def is_val(tree:Node)->bool: if type(tree.mark) == int: return True def show_node(tree:Node)->str: if is_var(tree) or is_val(tree): return str(tree.mark) else: #fill in example = Node("+",Node("*", leaf(2), # + leaf("x")), # / leaf(5)) # * 5 ..

I am looking to get Tree paths with required_sum 23: [[12, 7, 4], [12, 1, 10]] But instead I am getting Tree paths with required_sum 23: [[], []] my code is as follows… def findPaths(root, val): allPaths = [] findPathsHelper(root, sum, [], allPaths) return allPaths def findPathsHelper(currentNode, sum, currentPath, allPaths): if currentNode is None: return ..