A Visual Guide to How to Actually Invert a Binary Tree

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

_- Max Howell (@mxcl) _June 10, 2015

Given a binary tree like this:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Performing an inversion would result in:

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

The definition of a tree node is as follows:

function Node(val) {
  this.val = val;
  this.left = null;
  this.right = null;
}

This lesson was originally published at https://algodaily.com, where I maintain a technical interview course and write think-pieces for ambitious developers.


This is the famous question that Homebrew author Max Howell famously got wrong in a Google Interview. Hopefully this prevents you from having the same misfortune!

Let’s think about brute force — how would we do it without any clever algorithms? We can start with a very basic input as follows:

  1
 / \
2   3

So to invert it vertically, we’d start at 1, where there’s nothing to flip or swap, and it would stay put. We’ve now processed the first row.

Moving on to the second, we encounter 2 and 3, so we’d swap them and get:

Image for post

  1
 / \
3   2

Interesting, this seems to have inverted it! Is it as simple as swapping when there’s more than one node?

What if we had more than two nodes to swap per level though? If there was an additional level, it might look like this:

      1
     / \
    3   2
   / \   \
  4   5   3

That final row is currently directionally 4 -> 5 -> 3, but we’d want the outcome to be 3 -> 5 -> 4 to be properly inverted.

However, we can achieve this by doing two separate swaps. Notice that the below is what we’d get if we swapped 4 and 5 to obtain 5 -> 4, and then swapping 5 -> 4 with 3.

#data-structures #algorithms #programming #binary-tree #technical-interview

What is GEEK

Buddha Community

A Visual Guide to How to Actually Invert a Binary Tree

A Visual Guide to How to Actually Invert a Binary Tree

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

_- Max Howell (@mxcl) _June 10, 2015

Given a binary tree like this:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Performing an inversion would result in:

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

The definition of a tree node is as follows:

function Node(val) {
  this.val = val;
  this.left = null;
  this.right = null;
}

This lesson was originally published at https://algodaily.com, where I maintain a technical interview course and write think-pieces for ambitious developers.


This is the famous question that Homebrew author Max Howell famously got wrong in a Google Interview. Hopefully this prevents you from having the same misfortune!

Let’s think about brute force — how would we do it without any clever algorithms? We can start with a very basic input as follows:

  1
 / \
2   3

So to invert it vertically, we’d start at 1, where there’s nothing to flip or swap, and it would stay put. We’ve now processed the first row.

Moving on to the second, we encounter 2 and 3, so we’d swap them and get:

Image for post

  1
 / \
3   2

Interesting, this seems to have inverted it! Is it as simple as swapping when there’s more than one node?

What if we had more than two nodes to swap per level though? If there was an additional level, it might look like this:

      1
     / \
    3   2
   / \   \
  4   5   3

That final row is currently directionally 4 -> 5 -> 3, but we’d want the outcome to be 3 -> 5 -> 4 to be properly inverted.

However, we can achieve this by doing two separate swaps. Notice that the below is what we’d get if we swapped 4 and 5 to obtain 5 -> 4, and then swapping 5 -> 4 with 3.

#data-structures #algorithms #programming #binary-tree #technical-interview

Check if all the Nodes in a Binary Tree having common values are at least D distance apart

Given a Binary Tree and an integer D, the task is to check if the distance between all pairs of same node values in the Tree is ? D or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input:_ D = 7 _

                1
              /   \ 
             2     3
            / \   /  \ 
           4   3  4   4

Output:_ Yes _

Explanation:

_The repeated value of nodes are 3 and 4. _

_The distance between the two nodes valued 3, is 3. _

_The maximum distance between any pair of nodes valued 4 is 4. _

Therefore, none of the distances exceed 7

Input:_ D = 1 _

          3
         / \
        3   3
             \
              3

Output:_ No _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **

The idea is to observe that the problem is similar to finding the distance between two nodes of a tree. But there can be multiple pairs of nodes for which we have to find the distance. Follow the steps below:

  1. Perform the Post Order Traversal of the given tree and find the distance between the repeated pairs of nodes.
  2. Find the nodes that are repeated in the tree using unordered_map.
  3. For each repeated node of a particular value, find the maximum possible distance between any pair.
  4. If that distance is > D, print “No”.
  5. If no such node value is found having a pair containing that value, exceeding **D, **then print “Yes”.

#greedy #recursion #searching #tree #binary tree #frequency-counting #postorder traversal #tree-traversal

Jamel  O'Reilly

Jamel O'Reilly

1625935200

Data Visualization Guide - Over Time Visuals

Data Visualization Guide - Over Time Visuals

Download here: https://www.kratosbi.com/power-bi-community-of-practice

LINKS:
DATA GOD WEBSITE https://www.kratosbi.com/​
TWITTER https://twitter.com/KratosBi​
LINKEDIN https://www.linkedin.com/in/wagnerchris/​

DATA GOD MERCH https://merch.streamelements.com/krat…​

SUPPORT (Affiliate Links Below)

Buy Me A Coffee - Like this content? Feel free to buy me a coffee!
https://www.kratosbi.com/​

Value for Value - If you found value in this content, feel free to send me like value.
https://www.paypal.com/paypalme2/DataGod​

Stickermule deal link https://www.stickermule.com/unlock?re…​
Fiverr https://track.fiverr.com/visit/?bta=1…​

Recommended Books

Definitive Guide to DAX 2nd ed https://amzn.to/31mOXs1​
Definitive Guide to DAX 1st ed https://amzn.to/3jCKW9v​
Supercharge Power BI https://amzn.to/2LZfkeD​
Star Schema https://amzn.to/2VmSF1a​
The Data Warehouse Toolkit https://amzn.to/2OWMNYE​
Power Pivot and Power BI https://amzn.to/2D2yM9c​
Beginning DAX with Power BI https://amzn.to/330F0l3​
M is for (Data) Monkey https://amzn.to/3hAPYBx​

MY GEAR

Sony Alpha a6000 https://amzn.to/2LWW5T2​
Razer Kiyo https://amzn.to/2XzOnnr​
HyperX QuadCast https://amzn.to/2TErEp2​
Key Lights https://amzn.to/30PfcHo​
Background Lights https://amzn.to/3fAqJyj​
Elgato Stream Deck https://amzn.to/3enDGL8​
Corsair Void Pro https://amzn.to/3elY3Zn​
Corsair K70 RGB https://amzn.to/2ZESPE2​
Razer DeathAdder v2 https://amzn.to/2XuPMLZ​
Power BI Luchador https://amzn.to/3f7ZziF​

DESKTOP SETUP https://pcpartpicker.com/list/72chHB​
AMD Ryzen 7 2700X https://amzn.to/3fifquW​
MSI X470 ATX https://amzn.to/3fok36G​
2x 16GB Corsair Memory https://amzn.to/2ZqwwBz​
SSD 240GB - OS https://amzn.to/301JYL8​
SSD 1TB - Applications https://amzn.to/3eok6y0​
Seagate 1TB HD - Filestore https://amzn.to/2BU6UUI​
EVGA GeForce RTX 3070 tbd​
EVGA 750W 80+ Gold PSU https://amzn.to/2Du7Wa8​

LAPTOP SETUP
MSI GL75 i7 16GB 1660TI https://amzn.to/3ekmUML​
SteelSeries Rival 650 https://amzn.to/38QVFZ5​
Thermaltake 20 RGB https://amzn.to/32aMcuo​

Co-hosts
Captain America https://amzn.to/2BjQnst​
Casual Thor https://amzn.to/3eikcHW​
Hulk https://amzn.to/2X1BumQ​
Spider-Man https://amzn.to/3c6TmB0​
Groot https://amzn.to/2ZZAMrN​
Stan Lee https://amzn.to/30Mpgz5​
John Wick https://amzn.to/30X7wTo​
Tyler Durden(?) https://amzn.to/32VMxS8​
Maximus https://amzn.to/3jIloYu​
Karate Kid https://amzn.to/2CGAzRE​
Bluto - Toga https://amzn.to/2D5FcVa​
Bluto - College https://amzn.to/2WQGJW7​
Dr Who - 10th Doctor https://amzn.to/3hzDk5W​
Dr Who - 11th Doctor https://amzn.to/2OT5OLJ​
Dr Who - 12th Doctor https://amzn.to/30NKj4w​
Albert Einstein https://amzn.to/30LsRO2​
Vincent Van Gogh https://amzn.to/3g5BZU4​
Minsc & Boo https://amzn.to/2ZCXBlj​
Rick https://amzn.to/386G9bd​
Buzz https://amzn.to/3cZB6ed​
Woody https://amzn.to/2B0X0j3​
Bob https://amzn.to/2yy3WmW​
Linda https://amzn.to/2A9xojA​
Tina https://amzn.to/3elrQRO​
Gene https://amzn.to/36wqKQt​
Louise https://amzn.to/2M20aFI

#visualization guide #over time visuals

Arvel  Parker

Arvel Parker

1591177440

Visual Analytics and Advanced Data Visualization

Visual Analytics is the scientific visualization to emerge an idea to present data in such a way so that it could be easily determined by anyone.

It gives an idea to the human mind to directly interact with interactive visuals which could help in making decisions easy and fast.

Visual Analytics basically breaks the complex data in a simple way.

The human brain is fast and is built to process things faster. So Data visualization provides its way to make things easy for students, researchers, mathematicians, scientists e

#blogs #data visualization #business analytics #data visualization techniques #visual analytics #visualizing ml models

Double Order Traversal of a Binary Tree

Given a Binary Tree consisting of** N** nodes, the task is to print its Double Order Traversal.

Double Order Traversal_ is a tree traversal technique in which every node is traversed twice in the following order: _

  • Visit the Node.
  • Traverse the Left Subtree.
  • Visit the Node.
  • Traverse the Right Subtree.

Examples:

Input:
        1
      /   \
     7     3
    / \   /
   4   5 6
Output: 1 7 4 4 7 5 5 1 3 6 6 3 

Input:
        1
      /   \
     7     3
    / \     \
   4   5     6
Output: 1 7 4 4 7 5 5 1 3 3 6 6

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

The idea is to perform Inorder Traversal recursively on the given Binary Tree and print the node value on **visiting a vertex **and after the recursive call to the left subtree during the traversal.

Follow the steps below to solve the problem:

#data structures #recursion #tree #binary tree #inorder traversal #data analysis