Tags down


Are binary tree nodes passed by reference?

By : HelloHodges
Date : August 01 2020, 12:00 AM
I hope this helps you . The reference is passed by value. Meaning: the value of t2 is essentially just a pointer that happens to be the address of an object; when it is passed by value, the pointer (i.e. 4 or 8 bytes) is copied and passed in. It doesn't matter what the GetCount does with the local copy of the pointer: the change to the pointer is not observed at the call-site. However, if the method changes the object at the end of the pointer, then those changes will be observable.
code :

Share : facebook icon twitter icon

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree

By : Ravi
Date : March 29 2020, 07:55 AM
Does that help You should use a java.util.Stack to push and pop the nodes. Here is a modified version of your code:
code :
public class ReverseLevel {
    public static class Node {
        char id;
        Node left;
        Node right;

        public Node(char id) {
            this.id = id;
            left = null;
            right = null;

    static Node            root;
    static Queue<Node>     q;
    static int             index;
    static Stack<Character> stack = new Stack<Character>();

    public static void main(String[] args) {

        reverseLevels(root, 0);

        setLevels(root, 0);


    private static void printLevelbyLevel(Node root2) {
        q = new LinkedList<Node>();
        Queue<Node> nextLevel = new LinkedList<Node>();
        while (!q.isEmpty()) {
            Node n = q.remove();
            if (hasLeftChild(n)) {
            if (hasRightChild(n)) {
            if (q.isEmpty()) {
                while (!nextLevel.isEmpty()) {

    private static void reverseLevels(Node root, int level) {
        if (root == null) {
        reverseLevels(root.left, level + 1);
        if (level % 2 == 1) {
        reverseLevels(root.right, level + 1);


    private static void setLevels(Node root, int level) {
        if (root == null) {
        setLevels(root.left, level + 1);
        if (level % 2 == 1) {
            root.id = stack.pop();
        setLevels(root.right, level + 1);

    private static boolean hasRightChild(Node n) {
        if (n.right != null)
            return true;
        return false;

    private static boolean hasLeftChild(Node n) {
        if (n.left != null)
            return true;
        return false;

    private static void printNode(Node n) {
        System.out.print(n.id + " ");

    private static void createBT() {
        Node n1 = new Node('a');
        Node n2 = new Node('b');
        Node n3 = new Node('c');
        Node n4 = new Node('d');
        Node n5 = new Node('e');
        Node n6 = new Node('f');
        Node n7 = new Node('g');
        Node n8 = new Node('h');
        Node n9 = new Node('i');
        Node n10 = new Node('g');
        Node n11 = new Node('k');
        Node n12 = new Node('l');
        Node n13 = new Node('m');
        Node n14 = new Node('n');
        Node n15 = new Node('o');
        root = n1;
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n4.left = n8;
        n4.right = n9;
        n5.left = n10;
        n5.right = n11;
        n3.left = n6;
        n3.right = n7;
        n6.left = n12;
        n6.right = n13;
        n7.left = n14;
        n7.right = n15;

Binary Tree: List of List to Nodes and Reference with Recursion

By : user3276026
Date : March 29 2020, 07:55 AM
seems to work fine I think that is because you didn't define the action when l[1] and l[2] are not none. So when you pass the argument to the function, it puts 'a' to the key of root, and find that none of the conditions defined matched, then the function exists without doing any thing. Therefore the return value contains only 'a' . Try this:
if (l[1] and l[2]) == None: return bt.getRootValue() elif l[1] != None and l[2] == None: bt.left = ll2nr(l[1]) elif l[2] != None and l[1] == None: bt.right = ll2nr(l[2]) else: bt.left = ll2nr(l[1]) bt.right = ll2nr(l[2])

Python find lowest common ancestor of two nodes in a binary tree if not all of these nodes in the tree

By : Beni Bear
Date : March 29 2020, 07:55 AM
wish of those help We can count target node number and if it's 2, then we know both nodes are in the tree.
code :
class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        self.count = 0
        node = self.find(root, p, q)
        return node if self.count == 2 else None

    def find(self, node, p, q):
        if not node:
            return None
        if node in (p, q):
            self.count += 1
        left = self.find(node.left, p, q)
        right = self.find(node.right, p, q)
        return node if node in (p, q) or left and right else left or right

Trying to implement a vector passed by reference for use with a Binary Tree, what am I missing?

By : Yeekers
Date : March 29 2020, 07:55 AM
Hope that helps I am trying to write code to offload the inorder contents of a Binary Tree into a vector. IE: , Declare the argument as being passed by reference, e.g.
code :
void vectorExport(TreeNode *, vector<double>&);
void vectorExport(TreeNode *, vector<double>);

Find if two nodes are cousin nodes or sibling nodes in Binary Tree in JavaScript

By : Saurabh
Date : March 29 2020, 07:55 AM
this will help You could store the path to the wanted nodes and check the same length of the pathes and the last or the element before the last for getting the relation.
code :
function getPath(node, value, path = []) {
    if (!node) {
    if (node.value === value) {
        return path;
    return getPath(node.left, value, path.concat(node.value))
        || getPath(node.right, value, path.concat(node.value));

function findRelation(root, a, b) {
    var pathA = getPath(root, a),
        pathB = getPath(root, b);

    if (pathA.length !== pathB.length) {
    if (pathA.length && pathA[pathA.length - 1] === pathB[pathB.length - 1]) {
        return 'siblings';

    if (pathA.length > 1 && pathA[pathA.length - 2] === pathB[pathB.length - 2]) {
        return 'cousins';

var tree = { value: 3, left: { value: 2, left: { value: 1 } }, right: { value: 4, right: { value: 5 } } };

console.log(findRelation(tree, 2, 4));
console.log(findRelation(tree, 1, 5));
Related Posts Related Posts :
  • Conversion textbox text to data type int impossible
  • C# how to copy a file held inside a variable and paste it to different location?
  • c# alphanumeric list sorting by checking if list item contains
  • C# Method has 1 parameter but is invoked with 2 arguments
  • How to check whether string contains specific string in c#
  • UPnP hole punching can't use external IP to access device on local subnet
  • Is there a way to use a conditional when declaring method variables?
  • c# interop Excel - disable or remove automatic page breaks
  • How can I read in these commands from a file using?
  • Pinch and rotate around a point using MRTK and Hololens 1
  • Enhance performance to paint image, is SIMD perhapse a solution?
  • Which one has better performance, data types .NET or alias name data types C#?
  • HttpClient GetAsync taking ~2 seconds
  • linq fastest where method
  • Why can I read more than 254 characters with Console.ReadLine when the docs suggest that I shouldn't be able to?
  • C# - Delayed execution for a collection of items
  • How to display images in a CollectionView from a Base64 string?
  • Convert class object to class index
  • c# identifier expected in controller
  • Subscribe to multiple Observables and gets notified on the same thread
  • how to create windows service using crud operations in c# asp.net
  • Iterating through list of values in a dictionary c#
  • How to Implement Expo.io Push Notifications Send Through C#
  • Gridview columns not being set to width I enter
  • Impersonate User with Forms Authorization
  • ASP.NET MVC Impersonate not working with Forms Authentication
  • Found a potential bug in Moq library while selecting a constructor
  • I want to show a list but page is empty
  • Can this kind of concurrency problem be solved with async/await?
  • Yield keyword giving unexpected values for IEnumerable
  • Many DLLs in System namespace are included when installing library via Nuget
  • C# - range for textbox inputs
  • the Console.ReadLine() Doesn't set the string as the user input
  • Get notified when a variable changed
  • How to list players in room in Photon?
  • Link compile-time created resources to a class library
  • How to quickly choose numbers and weights in such a way as to obtain a specific weighted sum?
  • Downgrade .Net Core project from 3.1 to 2.2 - The type or namespace name 'IWebHostEnvironment' could not be found
  • c# multiplying array elements using system.numerics
  • Private methods vs local functions
  • C# project wants .NET 4.7.1 but I can't install it
  • Binding to object itself, doesn't update when individual property changes
  • remove duplicate from list in object c#
  • JSON.NET - find JObject by value regex in complex object?
  • Save an unknown sized array from unknown string length
  • Camera not centre on player sprite (monogame)
  • How to set a default type for T in a static generic method?
  • How can I insert multiple items of data into SQL using Web API?
  • Finding longest time in an array
  • How to find two quotation marks in a string
  • X509Certificate2 certification issue
  • Image format change in Azure
  • Why is there such big time difference in searching for element with higher index in ConcurrentBag?
  • Async Lambda Issue
  • C# SSIS Script Task - Add Thousand Seperator when formatting Excel column
  • How to check if a textbox starts with numeric value
  • c# Strange Pairs || I can't spot the difference between these 2 solutions?
  • str.Split(".") not working in C# interactive window but work in C# .Net core project?
  • How to do 3 conditions with if else C#
  • merge two lists of same type in c#
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org