binary-tree

Printing binary tree after initialising gives None value

Printing binary tree after initialising gives None value Question: I have a simple binary tree. I have intialized it using TreeNode class, but while printing out the values from the tree, it gives me None value as well. How to mitigate this? The code snippet is as follows: class TreeNode: def __init__(self, root=None, left = …

Total answers: 2

Construct Binary tree using dictionary

Construct Binary tree using dictionary Question: I have a dictionary of nodes and their children but I am stuck at inserting the children’s nodes to construct a binary tree. I am using the binary tree to get the preorder traversal and inorder traversal. input: Each key pair represent: root:(left child, right child) and can be …

Total answers: 1

find a path to a given node in binary tree – Python

find a path to a given node in binary tree – Python Question: I’m stuck finding the path of the given node so I can find the right side but not the left side. class Node: def __init__(self, key): self.left = None self.right = None self.val = key def path(self, k): if not self.val: return …

Total answers: 1

How do I return the children of recursive nodes in a python binary tree?

How do I return the children of recursive nodes in a python binary tree? Question: I currently have my code set up so I have a class that creates the first node of a binary search tree. Then, using a create_node method, I can add whatever node. How can I return the children of each …

Total answers: 1

Error in Level Order Traversal of Binary Tree

Error in Level Order Traversal of Binary Tree Question: What mistake have i done here ? def levelOrder(root): #Write your code here que = [] que.append(root) while que != []: coot = que.pop() print(coot.data,end=" ") if coot.left is not None: que.append(coot.left) if coot.right is not None: que.append(coot.right) OutPut Expected:1 2 5 3 6 4 MY_output: …

Total answers: 1

Problem with understanding the code of 968. Binary Tree Cameras

Problem with understanding the code of 968. Binary Tree Cameras Question: I am studying algorithms and trying to solve the LeetCode problem 968. Binary Tree Cameras: You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its …

Total answers: 1

Error when trying to debug LeetCode answer on local environment

Error when trying to debug LeetCode answer on local environment Question: I am working on LeetCode problem 199. Binary Tree Right Side View: Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. I want …

Total answers: 1

TypeError: isSameTree() missing 1 required positional argument: 'q' in implementing a leetcode solution locally

TypeError: isSameTree() missing 1 required positional argument: 'q' in implementing a leetcode solution locally Question: I tried to test a leetcode solution on my local PC (WSL, python3) to have a better understanding of the problem from https://leetcode.com/problems/same-tree/discuss/2594714/python-recurse-in-same-function I modified it as from typing import Optional class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = …

Total answers: 1

Why do I need if node after while queue to invert binary tree

Why do I need if node after while queue to invert binary tree Question: I was trying to invert a binary tree (i.e. swap all left & right children) class Node(object): def __init__(self, value=None, left=None, right=None): self.value = value self.left = left self.right = right def __repr__(self): return f"Node({self.value}, {self.left}, {self.right})" from collections import deque …

Total answers: 1